0

我刚刚开始使用 Scalaz。我正在尝试在其超类中为我的类型定义一个零。

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

我收到两个错误:

1)按原样,这会产生"error: class type required but Base.this.type found"

2)如果我将 this.type 的第二次出现更改为 Base (这没用),我得到

类型不匹配;
发现:
需要基础:Base.this.type

谁能帮我理解 this.type 出了什么问题?我真的不想将类型参数传递或覆盖到 Base。

4

1 回答 1

2

this.type和这个类的类型不一样。this.type是这个特定实例的单例类型。换句话说,以下内容将不起作用:

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 }

另一方面,这将:

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

从零开始,我会创建一个伴随对象并将每个零放入其中。

于 2012-10-05T03:14:00.270 回答