6

我有一个设计模式,其中有一个对象生成器(MorselGenerator及其子),其任何实例总是生成相同的确切类型的对象少量食物和它的孩子),但类型检查员不会让我对这些生成的物体中的两个或多个进行任何操作,相信它们可能会有所不同。斯卡拉类型推断未能注意到这些类型是相同的,无论它们是什么

我怎样才能让这个过去的类型检查?

trait Morsel 
{ 
    type M <: Morsel 
    def calories : Float 
    def + (v : M) : M 
} 

trait MorselGenerator 
{ 
    type Mg <: Morsel 
    def generateMorsel : Mg 
} 

class HotDog(c : Float, l : Float, w : Float) extends Morsel 
{ 
    type M = HotDog 
    val calories : Float = c 
    val length : Float = l  
    val width : Float = w 
    def + (v : HotDog) : HotDog = new HotDog(v.calories + calories, v.length + length, v.width + width) 
} 

class HotDogGenerator extends MorselGenerator 
{ 
    type Mg = HotDog 
    def generateMorsel : HotDog = new HotDog(500.0f, 3.14159f, 445.1f) 
} 

object Factory 
{ 
    def main (args : Array[String]) 
    { 
     val hdGen = new HotDogGenerator() 
     println(eatTwo(hdGen)) 
    } 

    def eatTwo (mGen : MorselGenerator) 
    { 
     val v0 : mGen.Mg = mGen.generateMorsel 
     val v1 : mGen.Mg = mGen.generateMorsel 
     v0 + v1       /// ERROR HERE 
    } 
} 

编译器生成以下编译错误

Generator.scala:43: error: type mismatch; 
found : v1.type (with underlying type mGen.Mg) 
required: v0.M 
     v0 + v1       /// ERROR HERE 
     ^one error found 


更新

这里是C++代码,那就是我想要做的更多或更少的等价物。请注意,eatTwo函数是完全多态的,并且没有提及特定派生类型的Morsel或MorselGenerator。

#include <stdlib.h> 
#include <stdio.h> 

template <class M> class Morsel 
{ 
public: 
    Morsel(float c) : calories(c) {} 
    float calories; 
    virtual M operator + (const M& rhs) const = 0; 
}; 

template <class M> class MorselGenerator 
{ 
public: 
    virtual M * generateMorsel() const = 0; 
}; 

class HotDog : public Morsel<HotDog> 
{ 
public: 
    HotDog(float c, float l, float w) : Morsel<HotDog>(c), length(l), width(w) {} 
    float length, width; 

    HotDog operator + (const HotDog& rhs) const 
    { return HotDog(calories+rhs.calories, length+rhs.length, width+rhs.width); } 
}; 

class HotDogGenerator : public MorselGenerator<HotDog> 
{ 
    HotDog * generateMorsel() const { return new HotDog(500.0f, 3.14159f, 445.1f); } 
}; 

/////////////////////////////////////////////// 

template <class MorselType> float eatTwo (const MorselGenerator<MorselType>& mGen) 
{ 
    MorselType * m0 = mGen.generateMorsel(); 
    MorselType * m1 = mGen.generateMorsel(); 
    float sum = ((*m0) + (*m1)).calories; 
    delete m0; delete m1; 
    return sum; 
} 

int main() 
{ 
    MorselGenerator<HotDog> * morselStream = new HotDogGenerator(); 
    printf("Calories Ingested: %.2f\n", eatTwo(*morselStream)); 
    delete morselStream; 
} 
+0

也许这将有助于:http://stackoverflow.com/questions/9198562/scala-self-type-and-this-type-in​​-collections-issue – tuxSlayer 2012-02-22 20:01:46

回答

2

这就是成员类型如何在Scala中工作的:它们只在外部对象(编译器已知)相同时才被认为是相等的。一种选择是使用类型参数代替:

trait Morsel[M <: Morsel] 
{ 
    def calories : Float 
    def + (v : M) : M 
} 

trait MorselGenerator[Mg <: Morsel] 
{ 
    def generateMorsel : Mg 
} 

... 
+0

嗯,这似乎并没有工作,实际上。首先,我们必须定义这些特征:'特质Morsel [Mg <:Morsel [Mg]]'等等,看起来奇怪的是圆形。通过这个设计,编译器比以前更加困惑,当我尝试添加两个Morsels(HotDogs)时。 – Fooberman 2012-02-18 21:42:36

+1

这并不比'class Hotdog ... {type M = HotDog ...}'更具循环性。请参阅http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern – 2012-02-19 15:57:30

4

错误是有道理的,因为在编译失败的方法,编译器不能保证您不添加到冰淇淋热狗。

在热狗

的+方法有助于突出的问题,其实你还没有覆盖的方法,而你已经添加了一个新问题:

def + (v : HotDog) : HotDog = new HotDog(v.calories + calories, v.length + length, v.width + width) 

你需要明确的类型被添加到有与“this”相同的类型。

定义小肉块这样,问题几乎是解决:

trait Morsel { 
    def calories : Float 
    def + (v : Morsel) : Morsel 
} 

最后一部分是覆盖+方法正确:

override def + (v : Morsel): Morsel = v match { 
    case hd: HotDog => new HotDog(hd.calories + calories, hd.length + length, hd.width + width) 
    case x => throw new IllegalArgumentException("eurgh!") 
} 

我不知道你是否能得到编译器阻止添加icecream和热狗,使用您提供的表单中的代码。

0

一个可能的解决方案(我已经更换了+add这里从+(String, String)望而却步,最终,+是确定):

trait Morsel[M <: Morsel[M]] { /// this 
    this: M =>      /// and this make the trick 
    def calories : Float 
    def add(v : M) : M 
} 

trait MorselGenerator[Mg <: Morsel[Mg]] 
{ 
    def generateMorsel : Mg 
} 

class HotDog(c : Float, l : Float, w : Float) extends Morsel[HotDog] 
{ 
    val calories : Float = c 
    val length : Float = l  
    val width : Float = w 
    override def add (v : HotDog) : HotDog = new HotDog(v.calories + calories, v.length + length, v.width + width) 
} 

class HotDogGenerator extends MorselGenerator[HotDog] 
{ 
    def generateMorsel : HotDog = new HotDog(500.0f, 3.14159f, 445.1f) 
} 

object Factory extends App 
{ 
    def eatTwo[M <: Morsel[M]](mGen : MorselGenerator[M]) = { 
    val v0 = mGen.generateMorsel 
    val v1 = mGen.generateMorsel 
    v0 add v1  
    } 

    val hdGen = new HotDogGenerator() 
    println(eatTwo(hdGen)) 
} 
0

和轻微的另一种变体:

trait MorselGenerator { 
    type M <: Morsel 

    trait Morsel { this: M => 
    def calories : Float 
    def add (v : M) : M 
    }  

    def generateMorsel : M 
} 

class HotDogGenerator extends MorselGenerator 
{ 
    type M = HotDog 

    class HotDog(c : Float, l : Float, w : Float) extends Morsel { 
    val calories : Float = c 
    val length : Float = l  
    val width : Float = w 
    def add (v : HotDog) : HotDog = new HotDog(v.calories + calories, v.length + length, v.width + width) 
    } 

    def generateMorsel: HotDog = new HotDog(500.0f, 3.14159f, 445.1f) 
} 

object Factory extends App 
{ 
    val hdGen = new HotDogGenerator() 

    hdGen.generateMorsel add hdGen.generateMorsel add hdGen.generateMorsel 

    produceDouble(hdGen) 

    def produceDouble(gen: MorselGenerator): MorselGenerator#Morsel = { 
    gen.generateMorsel add gen.generateMorsel 
    } 
} 

可能不太有用,但它可能会显示问题出在哪里。 Scala具有“路径依赖”类型,因此,即使obj1.type == obj2.type,obj1.Type和obj2.Type也是不同的类型。