Escaping underscore for Java interoperability in Scala -


suppose i've got scala code calls java library uses _ identifier (and do—it's long story). here's simplified example:

public class stupidunderscore {   public static string _() { return "please give me real name!"; } } 

no problem, right? escape it:

scala> stupidunderscore.`_` res0: string = please give me real name! 

and has worked, until tried update scala 2.10.2 morning:

scala> stupidunderscore.`_` <console>:1: error: wildcard invalid backquoted identifier        stupidunderscore.`_`                         ^ 

this due a change showed in 2.10.1 , fixes this issue. commit message:

prohibit _ identifier, can bring badness.

well, sure, don't know why means can't escape it—i thought that's backquotes for.

am going have write java wrapper work? there other way can refer java library's _ method in scala?


as footnote, i've confirmed it's possible work around issue without writing new java:

object awfulmacrohack {   import scala.language.experimental.macros   import scala.reflect.macros.context    def _impl(c: context) = {     import c.universe._     c.expr[string](       select(ident(newtermname("stupidunderscore")), newtermname("_"))     )   }    def `i'm not named _!` = macro _impl } 

and then:

scala> awfulmacrohack.`i'm not named _!` res0: string = please give me real name! 

i'm not sure less horrible java helper solution, though.

since backticks the mechanism java interop on identifiers (e.g. thread.yield()), doubt there's way without using reflection. i'd best bet (until bug fixed) write static helper method in java access _.

i know totally ridiculous, it's still better using reflection. broke java interop patch, they'll have address issue eventually. until do, however, think it's broken.


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -