scala - Pattern for a class that is a list of itself -
consider following:
object main { case class foo(bar: int) extends foolist { val self: list[foo] = :: nil } abstract class foolist { val self: list[foo] def ~(that: foo) = { val list = self :+ that; new foolist { val self = list } } } def main(args: array[string]): unit = { val foo = foo(1) ~ foo(2) ~ foo(3) println(foo.self) } } could line:
{ val list = self :+ that; new foolist { val self = list } } be simplified in way? i'd write like:
new foolist { val self = this.self :+ } // won't compile it seems boil down being able refer differently-scoped identifiers has same name. there mechanism that?
this solves scoping issue. if understand correctly that's want.
abstract class foolist { outer => val self: list[foo] def ~(that: foo) = { new foolist { val self = outer.self :+ } } } answer: yes. self-types can used aliases outer scopes.
Comments
Post a Comment