Foo*_*man 6 scala type-inference abstract-type static-polymorphism dependent-type
我有一个设计模式,其中有一个对象生成器(MorselGenerator及其子代),其中任何实例总是生成相同的确切类型的对象(Morsels及其子代),但类型检查器将不允许我执行任何操作两个或更多这些生成的对象,相信它们可能不同.
如何通过类型检查器?
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
}
}
Run Code Online (Sandbox Code Playgroud)
编译器生成以下编译错误
Generator.scala:43: error: type mismatch;
found : v1.type (with underlying type mGen.Mg)
required: v0.M
v0 + v1 /// ERROR HERE
^ one error found
Run Code Online (Sandbox Code Playgroud)
这里的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;
}
Run Code Online (Sandbox Code Playgroud)
这正是成员类型在 Scala 中的工作方式:只有当外部对象(编译器已知)相同时,它们才被视为相等。一种选择是使用类型参数:
trait Morsel[M <: Morsel]
{
def calories : Float
def + (v : M) : M
}
trait MorselGenerator[Mg <: Morsel]
{
def generateMorsel : Mg
}
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
209 次 |
| 最近记录: |