I have recently learnt about cake pattern and the differences between the uses of self => and self:T => (see here). The difference between these technicalities and real Scala code as remarked here continue to create me problems. For instance, see the following code snippet taken from the Inox project:
trait Trees
extends Expressions
with Constructors
with Extractors
with Types
with Definitions
with Printers
with TreeOps { self =>
...
val interpolator: Interpolator { val trees: Trees.this.type } = new {
protected val trees: Trees.this.type = Trees.this
} with Interpolator
...
}
In summary, the whole snippet does not make much sense to me (and it is a pattern frequently repeated in the code), let me explain:
- What is this syntax?
val interpolator: Interpolator { ... }
up to now I wrote val name: Type = value, here there is no equal.
Trees.this.typeshould be a type, but what type? It should be defined in Trees trait, and thethiscontext which I bet is different from thetrait Treescontext (related to problem 1). I also looked on file Interpolators but there does not seem to be a type element.The greatest line is
protected val trees: Trees.this.type = Trees.this.
Can anybody explain me what is going on here?
It's a declaration of variable
interpolatorwith typeInterpolator { val trees: Trees.this.type }. The typeInterpolator { val trees: Trees.this.type }is a subtype ofInterpolator, but refined with the additional restriction thattreesis not just someTrees, but instead one concrete instance ofTrees, namely the one of the singleton typeTrees.this.type. There is an equal-=-symbol: between the typeInterpolator { val trees: Trees.this.type }and thenew { ... } with Interpolator.Shorter example which demonstrates the syntax in isolation:
Trees.this.typeis the singleton type of the valuethis. There is only one value of this type:Trees.this.Shorter example that demonstrates usage of
.type:You are setting the value
treestoTrees.this. You also guarantee to the compiler thattreesis not just someTrees, but thattreesis the singleton valueTrees.thisof the singleton typeTrees.this.type, which is a strict subtype ofTrees.