score:0

this is indeed unfortunate, and also happens in scala 2.9.2; you need to add the type there, though, so it's val x: x = new x(x). i think the compiler should clearly reject this as it can't succeed with eager argument v.

i don't know what your exact scenario is, but it seems to be a data structure. maybe an approach like the following works for you

trait top
trait x extends top { def v: top }

object toploop extends x { def v: top = this }
class y( x: x ) extends top { def v: top = x }

if you want to use pattern matching, you can furthermore add sealed to the traits and case to the object and class.

score:5

lung,

how did your code with lazy and call-by-name look like? it works for me like this:

class top
class x(_v: => top) extends top {
  lazy val v = _v
}

scala> lazy val x: x = new x(x)
x: x = <lazy>

scala> x.v
res3: top = x@422d15ad

Related Query

More Query from same tag