Tuesday, January 19, 2010

Scala string reversal gotcha

Be careful when invoking Scala's RichString methods on a java.lang.String (via implicit conversion):

scala> "8008".reverse == "8008"
res0: Boolean = false
scala> "8008".reverse
res1: scala.runtime.RichString = 8008
scala> "8008"
res2: java.lang.String = 8008
"8008".reverse == "8008".toSeq
res3: Boolean = true

That toSeq call is unfortunate, but necessary.  I suppose this is the price we pay for having Scala String literals be typed as Java Strings.

No comments: