I've been trying to generate an SBT build for an open source project that I'd like to reference from within my project, and I ran into what seems to be a compiler bug.
The following code compiles and runs as expected in eclipse/scala-ide, but the scala 2.10.6 compiler is unable to digest it:
package foo
import scala.language.dynamics
object Caller extends App {
val client = new Client() // initialise an R interpreter
client.x = 1.0
}
class Client extends Dynamic {
var map = Map.empty[String, Any]
def selectDynamic(name: String) = map get name getOrElse sys.error("field not found")
def updateDynamic(name: String)(value: Any) { map += name -> value }
}
Here's my build.sbt:
scalaVersion := "2.10.6"
libraryDependencies++= Seq(
"org.scalanlp" %% "breeze" % "0.12"
)
When I specify scalaVersion := 2.10.6, I get the following compile error:
[error] /home/philwalk/dynsbt/src/main/scala/foo/Caller.scala:8: type mismatch;
[error] found : foo.Caller.client.type (with underlying type foo.Client)
[error] required: ?{def x: ?}
[error] Note that implicit conversions are not applicable because they are ambiguous:
[error] both method any2Ensuring in object Predef of type [A](x: A)Ensuring[A]
[error] and method any2ArrowAssoc in object Predef of type [A](x: A)ArrowAssoc[A]
[error] are possible conversion functions from foo.Caller.client.type to ?{def x: ?}
[error] client.x = Seq("a","b","c")
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 3 s, completed May 3, 2016 11:03:08 AM
With scalaVersion := 2.11.8, no problems, although I need to cross-compile, so that's not a work-around.
Another clue is that I can hide the problem by changing this line of code:
client.x = 1.0
to this:
client.xx = 1.0
I also see the problem when I compile directly with scalac 2.10.6.
As a workaround, I could refactor the project to use field names longer than a single character, although because it's not my project, I'm somewhat constrained on what I can accept as a workaround. Also, it's a breeze.linalg project and it would be a serious limitation to disallow single character matrix and vector names.
It took a few hours to boil the problem down to this code fragment from a larger project, and I would prefer not to impose limitations on the scala 2.10 version of this open source library. Since this bug seems to have been fixed in scala 2.11, I'm assuming it was decided not to backport the fix to 2.10.
I changed the title to reflect the existence of a workaround (longer field name).