Created
March 24, 2011 08:20
-
-
Save mads-hartmann/884739 to your computer and use it in GitHub Desktop.
Possible bug in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.reversebackup | |
import scala.xml._ | |
import scala.xml.transform._ | |
import scala.xml.XML | |
/* | |
This snippet creates two methods: removeAllNamedTagsTransformer & createRewriteRule. | |
They both create a RuleTransformer which removes all tags with a specific name. | |
However, it seems that it doesn't work if the name of the parameter is 'name'. | |
removeAllNamedTagsTransformer(name: String) doesn't do any transformations but the | |
same method named createRewriteRule(label: String) does. If you change the parameter | |
name to something other than 'name' it works. Is this a bug? | |
*/ | |
object Main4 { | |
// This one doesn't work | |
def removeAllNamedTagsTransformer(name: String) = { | |
new RuleTransformer(new RewriteRule { | |
override def transform(n: Node): Seq[Node] = n match { | |
case Elem(prefix, `name`, attribs, scope, _*) => NodeSeq.Empty | |
case other => other | |
} | |
}) | |
} | |
// This one works. | |
def createRewriteRule(label: String) = { | |
new RuleTransformer(new RewriteRule { | |
override def transform(n: Node): Seq[Node] = n match { | |
case Elem(prefix, `label`, attribs, scope, _*) => NodeSeq.Empty | |
case other => other | |
} | |
}) | |
} | |
def main(args: Array[String]): Unit = { | |
val data1 = (xml \\ "person").map(createRewriteRule("id")) | |
val data2 = (xml \\ "person").map(removeAllNamedTagsTransformer("id")) | |
println(data1 == data2) | |
} | |
val xml = <people type="array"><person> | |
<author-id type="integer">443736</author-id> | |
<background nil="true"></background> | |
<company-id type="integer">64676331</company-id> | |
<created-at type="datetime">2011-03-17T10:13:50Z</created-at> | |
<first-name>Mads</first-name> | |
<group-id nil="true" type="integer"></group-id> | |
<id type="integer">64723802</id> | |
<last-name>Hartmann Jensen</last-name> | |
<owner-id nil="true" type="integer"></owner-id> | |
<title>Software Developer</title> | |
<updated-at type="datetime">2011-03-17T19:18:19Z</updated-at> | |
<visible-to>Everyone</visible-to> | |
<contact-data> | |
<phone-numbers type="array"></phone-numbers> | |
<addresses type="array"></addresses> | |
<email-addresses type="array"> | |
<email-address> | |
<address>[email protected]</address> | |
<id type="integer">31204049</id> | |
<location>Work</location> | |
</email-address> | |
<email-address> | |
<address>[email protected]</address> | |
<id type="integer">31204050</id> | |
<location>Home</location> | |
</email-address> | |
</email-addresses> | |
<web-addresses type="array"></web-addresses> | |
<instant-messengers type="array"></instant-messengers> | |
<twitter-accounts type="array"></twitter-accounts> | |
</contact-data> | |
</person> | |
</people> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Comment from Rex on the scala-language ML:
The API for RewriteRule includes:
val name : String
a name for this rewrite rule
so the variable name that you passed in is no longer the one used in that scope.
It's not a bug; it's just a standard name collision.