0

I'm just getting started with Scalaz. I'm trying to define a Zero for my type in its superclass.

class Base { 
  implicit def BaseZ: Zero[this.type] = zero(classOf[this.type].newInstance() )
}

class Child extends Base

~Option(null:Child) //trying to make this produce: new Child

I'm getting two errors:

1) As is, this produces "error: class type required but Base.this.type found"

2) If I change the second occurrence of this.type to Base (which isn't useful), I get

type mismatch;
found : Base
required: Base.this.type

Can anyone help me understand what's going wrong with this.type here? I really don't want to have to pass or override a type parameter to Base.

scalapeno
  • 833
  • 1
  • 6
  • 13

1 Answers1

2

this.type is not the same as the type of this class. this.type is the singleton type of this particular instance. In other words, the following will not work:

scala> class X { def x:this.type=new X }
<console>:7: error: type mismatch;
 found   : X
 required: X.this.type
       class X { def x:this.type=new X }

On the other hand, this will:

scala> class X { def x:this.type=this }
defined class X

As of the Zeros, I'd create a companion object and put each zero in it.

pedrofurla
  • 12,763
  • 1
  • 38
  • 49