2012-10-04 57 views
0

我刚刚开始使用Scalaz。我试图在它的超类中为我的类型定义一个Zero。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.this.type

谁能帮助我了解与this.type这里回事?我真的不想传递或重写一个类型参数到基地。

回答

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 

作为零点,我想创建一个同伴对象,并把每个零它。

+0

那么,没有办法说延伸基地的每个孩子的零应该产生一个新的孩子吗? – scalapeno

+0

您可以尝试使用清单 – pedrofurla

+0

如何使用清单来完成此操作? – scalapeno