I'm trying to declare attribute of parametrized type inside anonymous class. This works in Java, in Scala (2.9) however I get compile error:
Parameter type in structural refinement may not refer to an abstract type defined outside that refinement
This is the code:
object DemoFail extends App {
def it[T <: AnyRef](x: T) = new Iterator[T] {
var i = x // here is the error
def next = i
def hasNext = true
}
for (i ← it(int2Integer(4))) println(i)
}
I can get it to work by "erasing" types manually:
object DemoOK extends App {
def it[T <: AnyRef](x: T) = new Iterator[T] {
var i: AnyRef = x
def next = i.asInstanceOf[T]
def hasNext = true
}
for (i ← it(int2Integer(4))) println(i)
}
So the question is: why can't the compiler do it for me ?
iwould happen through Java reflection, right?