The nearest thing to
case class Foo(lazy next: Foo)
that I have been able to come up with is
class Foo(_next: =>Foo) {
lazy val next = _next
}
object Foo {
def apply(next: =>Foo) = new Foo(next)
def unapply(foo: Foo) = Some(foo.next)
}
I found a listed issue add lazy parameters so I guess it will be added someday. In the meantime, does anyone know a cleaner trick than the above?
Maybe scalaz.Need
? This was suggested to me in this answer.
scala> case class Foo(next: Name[Foo])
defined class Foo
scala> lazy val x: Foo = Foo(Need(y)); lazy val y: Foo = Foo(Need(x))
x: Foo = <lazy>
y: Foo = <lazy>
scala> x.next.flatMap(_.next).value eq x
res61: Boolean = true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With