0

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

Srinivas
  • 2,010
  • 7
  • 26
  • 51
  • self-type. Basically meaning, the entity which implements this trait is also another Trait, but this trait itself is not that Trait (so you shouldn't extend it) – SwiftMango Oct 01 '18 at 17:45

1 Answers1

5

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.

Bogdan Vakulenko
  • 3,380
  • 1
  • 10
  • 25