Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using this keyword to inherit? [duplicate]

Tags:

this

scala

Possible Duplicate:
What is the difference between scala self-types and trait subclasses?

From example in scalatest site. There is one particular thing I don't really understand

trait FunSuiteStackBehaviors { 
                   this: FunSuite => //This line
                         def a() {}
                         def b() {}
}

class StackFunSuite extends FunSuite with FunSuiteStackBehaviors {}

As far as I understand, it seems like they try to assign some defs into a trait. But what does this: FunSuite => part do ? I tried to use extends FunSuite instead like

trait FunSuiteStackBehaviors extends FunSuite { 
                         def a() {}
                         def b() {}
}

class StackFunSuite extends FunSuite with FunSuiteStackBehaviors {}

and I still end up with same result. Are they the same thing ?

like image 659
Tg. Avatar asked Nov 14 '22 13:11

Tg.


1 Answers

the this: => XXXX is called a self type annotation http://www.scala-lang.org/node/124 Basically, you're specifying the type of "this" (current object) to the type specified. Kind of a "cast from the inside"

like image 111
GClaramunt Avatar answered Dec 15 '22 09:12

GClaramunt