I came across something like this recently
object example {
type DButil = AnyRef {
val a: Int
def b: String
}
}
Things that confused me is that usage of AnyRef here, later I dig a bit more, and I find you can also do this:
object apple {}
object example {
type DButil = apple.type {
val a: Int
def b: String
}
}
I've never seen this pattern before, any insights and comments are appreciated.
DButilis anything that has anafield of type Int, and a methodbthat receives nothing a returns a String; in the second example, such type also has to be a subclass ofapple. Those are, IMHO, a bad feature; structural types relay in reflection (as such are insecure, slow, unsafe and less portable) and are not as flexible as they say they are. In my experience, they are used mostly by newcomers that come from languages like Python or JS and do not know how to properly model Polymorphism. - Luis Miguel Mejía Suárez