Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone explain me this code in scala ?

Tags:

scala

Can anyone explain me the Scala code written below?

 trait A extends B {
  self =>
  type S >: self.type <: A
  def instance: S = self
}
like image 959
Manjot Kaur Avatar asked Dec 22 '25 09:12

Manjot Kaur


1 Answers

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.

like image 74
volia17 Avatar answered Dec 24 '25 02:12

volia17



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!