Considérez ce qui suit:Motif pour une classe qui est une liste de se
object Main
{
case class Foo(bar: Int) extends FooList {
val self: List[Foo] = this :: 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)
}
}
cette ligne pourrait:
{ val list = self :+ that; new FooList { val self = list } }
être simplifiée de quelque façon? Je voudrais écrire quelque chose comme:
new FooList { val self = this.self :+ that } // won't compile
Il semble faire bouillir de pouvoir se référer à des identificateurs différemment scope qui a le même nom. Y a-t-il un mécanisme pour cela?
C'est excellent, merci. –