Knut Arne Vedaa Knut Arne Vedaa - 13 days ago 4
Scala Question

Weird Scala bug related to implicit conversions on default constructor parameters

Consider the file bug.scala:

package sandbox

object Foo {
implicit def stringToInt(s: String) = 5
}

import Foo._

class Boo(val a: Int = "foo" / 3) {
val b: Int = "foo" / 3
def c(d: Int = "foo" / 3) = d
}


It defines an implicit conversion, imports it, and uses it in three slightly different scenarios. Compile it:

E:\prog\scala\test>scalac bug.scala
bug.scala:9: error: value / is not a member of java.lang.String
class Boo(val a: Int = "foo" / 3) {
^
one error found


The implicit conversion seems not to be working when used for a default constructor parameter, but works for the other scenarios.

Now watch this:

package sandbox

object Foo {
implicit def stringToInt(s: String) = 5
}

object dummy

import Foo._
import dummy._

class Boo(val a: Int = "foo" / 3) {
val b: Int = "foo" / 3
def c(d: Int = "foo" / 3) = d
}


We've simply added an empty object and imported it. Now the file compiles without errors!

I see two possibilities here: 1) My head is messing with me. 2) There's a bug in the compiler. Can anyone confirm it's the latter?

(Using Scala 2.8.1.)

Answer

Definitely a bug. I've lodged it on your behalf.

https://issues.scala-lang.org/browse/SI-4141