6

Mam wzór projektu tutaj, gdzie istnieje generator obiektów (MorselGenerator i jego dzieci), z których każda generuje zawsze taki sam dokładny typ obiektu (Morsels i jego dzieci), ale sprawdzanie typu nie pozwoli mi wykonywać żadnych operacji na dwóch lub więcej z tych wygenerowanych obiektów, uważając, że mogą być inne.Wnioskowanie typu Scala nie zauważa, że ​​te typy są identyczne, niezależnie od tego, czy są one

Jak uzyskać to za pomocą sprawdzania typu?

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 
    } 
} 

Kompilator generuje następujący błąd kompilacji

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


Aktualizacja

Oto kod C++, który jest mniej więcej odpowiednikiem, co usiłuję zrobić. Zauważ, że funkcja eatTwo jest w pełni polimorficzna i nie ma odniesienia do określonych pochodnych typów Morsel lub 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

może to pomoże: http://stackoverflow.com/questions/9198562/scala-self-type-and-this-type-in-collections-issue – tuxSlayer

Odpowiedz

2

To jest po prostu jak typy członkowskie działają w Scala: są one uznawane za równe tylko wtedy, gdy zewnętrzne obiekty (znany kompilator być) takie same. Jedną z opcji jest wykorzystanie parametrów typu Zamiast:

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

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

... 
+0

Cóż, to chyba nie działa.Po pierwsze, musimy zdefiniować cechy w ten sposób: 'cecha Morsel [Mg <: Morsel [Mg]]' itp., Która wydaje się dziwnie kolista. Po wykonaniu tego projektu kompilator jest bardziej zdezorientowany niż kiedykolwiek, kiedy próbuję dodać dwa kęsy (HotDogs). – Fooberman

+1

To nie jest bardziej okrągłe niż 'klasa Hotdog ... {typ M = HotDog ...}'. Zobacz http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern –

4

Błąd ma sens, ponieważ w metodzie gdzie kompilacja nie powiedzie się, kompilator nie może zagwarantować, że nie dodajemy do lodów hotdog.

Znak + metoda hotdog pomaga podkreślić problem, aw rzeczywistości nie mają zastąpić metodę, a dodasz nowe:

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

jawnie trzeba typ dodawanych mieć ten sam typ co "to".

Definiowanie kęs jako takie, a problem jest prawie rozwiązany:

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

Końcowa część to nadpisać metodę + prawidłowo:

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!") 
} 

Nie jestem pewien, czy można dostać kompilator, aby zapobiec dodawaniu lodów i hot-dogów, używając kodu w dostarczonej formie.

0

Jednym z możliwych rozwiązań (I zostały zastąpione + z add tutaj, aby trzymać się z dala od +(String, String), w końcu, + jest ok):

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

i nieznaczne inny wariant:

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 
    } 
} 

prawdopodobnie mniej przydatne, ale może pokazać, gdzie jest problem. Scala ma typy "zależne od ścieżki", więc obj1.Type i obj2.Type są różnymi typami, nawet jeśli obj1.type == obj2.type.

Powiązane problemy