Manjot Kaur
Manjot Kaur

Reputation: 31

Can anyone explain me this code in scala ?

Can anyone explain me the Scala code written below?

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

Upvotes: 2

Views: 90

Answers (1)

volia17
volia17

Reputation: 938

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.

Upvotes: 8

Related Questions