I am reading about scalatest and I am coming across this syntax a lot
trait Sample {
self : FlatSpec =>
}
What does this mean? Please explain with an example
I am reading about scalatest and I am coming across this syntax a lot
trait Sample {
self : FlatSpec =>
}
What does this mean? Please explain with an example
It's called self-type. It means trait Sample
can access all members from FlatSpec
, but when you create any instance based on this trait you must mixin
(combine) this instance with instance of FlatSpec
. You are not able to create an instance of Sample
without providing implementation of FlatSpec
because Sample
may use methods from there.
This may look a bit like inheritance but it's not. See this answer
Note: self
is just a reference to current object not a syntax element. You can use any word instead of self
.