Friday 20 November 2015

How to use this.type in Scala

dfdf


scala> abstract class Abstract0 {
     |   def setOption(j: Int): this.type
     | }
  | 
  | class Concrete0 extends Abstract0 {
     |   var i: Int = 0
     |   def setOption(j: Int) = {
     |    i = j; 
     |    this
     |    }
     | }

scala> (new Concrete0).setOption(1).setOption(1)
res72: Concrete0 = Concrete0@a50ea1

this.type is a singleton type - the type of one specific instantiated Abstract0
In above example, setOption() returns the actual type used, rather than Abstruct0.




No comments:

Post a Comment