Scala 类型推断没有注意到这些类型是相同的,无论它们是什么

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

编译器产生如下编译错误
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;
}

最佳答案

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

HotDog 中的 + 方法有助于突出问题,实际上您并没有覆盖该方法,而是添加了一个新方法:

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

您明确需要添加的类型与“this”具有相同的类型。

像这样定义 Morsel,问题就基本解决了:
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!")
}

我不确定您是否可以使用您提供的表格中的代码让编译器阻止添加冰淇淋和热狗。

关于Scala 类型推断没有注意到这些类型是相同的,无论它们是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9343817/

相关文章:

scala - 如何在不使用 A 的情况下将效果附加到 Resource[F, A] 中的 F?

scala - 使用来自 JSON 字符串的嵌套映射

typescript - 键在应该用于索引类型时却不能使用

斯卡拉 3 : "assertion failed: denotation class Int invalid in run 1."

scala - Finagle + 节俭 : Count method invocations

syntax - 无法从 SPRS 库中初始化 TriMat 矩阵

language-agnostic - 返回类型推断有缺点吗?如果是,它们是什么?

scala 类构造函数和抽象类型

scala - 为什么不可能覆盖已经实现的抽象类型?