Between Scala (2.12.8) self-type
trait T {
this: Any =>
}
and this
trait T {
this: Any
}
what's the semantics difference?
In other words, what is the purpose of this: Any (in the second snippet)?
I expected the compiler to yell I should not declare this when compiling the second code snippet, but I get this warning instead:
Warning:(2, 9) a pure expression does nothing in statement position
multiline expressions may require enclosing parentheses
this: Any
The keyword
thisis an expression of typeT.Tis subtype ofAny, because everything is subtype ofAny. Hence you can explicitly ascribe typeAnyto the expressionthis. It is valid to have expressions in the initializer, so you can write the expressionthis: Anyin the body ofT.You might as well have written
or
In both cases,
42andthiswould be just expressions with explicit type ascription that simply don't do anything. They are not declarations, and they have nothing to do with the self type.