, :
object DemoFail extends App {
// The same with an explicit (non structural) return type
// vvvvvvvvvvvvv
def it[T <: AnyRef](x: T): Iterator[T] =
new Iterator[T] {
var i = x // no more error
def next = i
def hasNext = true
}
for (i ← it(int2Integer(4))) println(i)
}
Indeed, a method iton an object DemoFaildoes not have an explicit return type. Therefore, this type of return is deduced by the compiler.
Here, since you are redefining existing elements and adding a new one to Iterator[T], the return type of the return method itis the structural type of the form Iterator[T] with Object {def next: T; var i : T; def hasNext: Boolean}(as an IDE might seem, like IntelliJ),
Thus, you define a method whose return type is a structural type that uses the abstract type of the same method. This is what worries the scalp (a structural type with the same abstract type method).
source
share