2

Can anyone explain me the Scala code written below?

 trait A extends B {
  self =>
  type S >: self.type <: A
  def instance: S = self
}
1

1 Answer 1

8

The line self => is a self type declaration, with no type, so in trait A, self is a synonym of this.

The line type S >: self.type <: A defines a type S with upper bound and lower bound to it. So S must be a subtype of A and a supertype of type of self (which is a concrete type of A)

Then the line def instance: S = self defines a method without arguments, a kind of property named instance, which return this casted to S.

Finally, why this code, i don't know, i don't like this kind of code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.