Generics 使用类型级计算时类型推断/类型检查失败

Generics 使用类型级计算时类型推断/类型检查失败,generics,scala,types,alias,units-of-measurement,Generics,Scala,Types,Alias,Units Of Measurement,我在使用文件中定义的功能中的度量单位时遇到问题 对于这个问题的其余部分,我将使用一个简化方案,只有一个单元类型,即长度 那么,在现实中,一种类型看起来像什么呢 Quantity[_1, _0, _0, _0, _0, _0, _0] ^ ^ ^ ^ ^ ^ ^ | | | | | | | | Mass | Crncy.| Mol | Length Time

我在使用文件中定义的功能中的度量单位时遇到问题

对于这个问题的其余部分,我将使用一个简化方案,只有一个单元类型,即长度

那么,在现实中,一种类型看起来像什么呢

Quantity[_1, _0, _0, _0, _0, _0, _0] 
          ^   ^   ^   ^   ^   ^   ^
          |   |   |   |   |   |   |
          | Mass  | Crncy.|  Mol  |
       Length   Time    Temp. Lum.Intensity
这将足以证明问题:

Quantity[_1]
          ^
          |
       Length
一旦需要推断类型,问题就开始了

考虑以下示例(同时查看以下代码):

我在最后一行得到一个错误,它说:

type mismatch;
  found :
    scalax.units.Units.Quantity[
      scalax.units.Subtractables.-[
        scalax.units.Integers._2,
        scalax.units.Integers._1
      ]
    ]

  required:
    scalax.units.Units.Quantity[
      scalax.units.Integers._1
    ]
当“减去维度”时,编译器似乎无法判断手头的类型是否等于
Quantity[\u 1]
。G从一个区域到另一个区域,如
(1)


(很抱歉没有将整个代码粘贴到这里,这太多了。我试图最小化我的示例,但失败了。这就是为什么我只是链接到它。)

MInt
trait中缺少
Sub
from
Subtractable
。一个简单的定义是,在
MSucc
MNeg
中减去一个类型时,执行负加法

sealed trait MInt extends Visitable[IntVisitor] with Addable with Subtractable {
  type AddType = MInt
  type SubType = MInt
  type Add[I <: MInt] <: MInt
  type Sub[I <: MInt] <: MInt
  type Neg <: MInt
  type Succ <: MInt
  type Pre <: MInt
}

final class _0 extends Nat {
  type Add[I <: MInt] = I
  type Sub[I <: MInt] = I#Neg
  type AcceptNatVisitor[V <: NatVisitor] = V#Visit0
  type Neg = _0
  type Succ = MSucc[_0]
  type Pre = Succ#Neg
}

final class MSucc[P <: Nat] extends Pos {
  type This = MSucc[P]
  type Add[N <: MInt] = P#Add[N]#Succ
  type Sub[N <: MInt] = Add[N#Neg]
  type AcceptNatVisitor[V <: NatVisitor] = V#VisitSucc[P]
  type Neg = MNeg[This]
  type Pre = P
  type Succ = MSucc[This]
}

final class MNeg[P <: Pos] extends MInt {
  type Add[N <: MInt] = P#Add[N#Neg]#Neg
  type Sub[N <: MInt] = Add[N#Neg]
  type Accept[V <: IntVisitor] = V#VisitNeg[P]
  type Neg = P
  type Succ = P#Pre#Neg
  type Pre = P#Succ#Neg
}
扩展Visitable[IntVisitor]和Addable with Subtractable{
类型AddType=MInt
类型子类型=薄荷

键入Add[I是的,这是修复。我已将这两个修复提交给MetaScala回购。
Quantity[_2 - _1] <<not equal to>> Quantity[_1]
Quantity[_1 + _1] <<equal to>> Quantity[_2]
sealed trait MInt extends Visitable[IntVisitor] with Addable with Subtractable {
  type AddType = MInt
  type SubType = MInt
  type Add[I <: MInt] <: MInt
  type Sub[I <: MInt] <: MInt
  type Neg <: MInt
  type Succ <: MInt
  type Pre <: MInt
}

final class _0 extends Nat {
  type Add[I <: MInt] = I
  type Sub[I <: MInt] = I#Neg
  type AcceptNatVisitor[V <: NatVisitor] = V#Visit0
  type Neg = _0
  type Succ = MSucc[_0]
  type Pre = Succ#Neg
}

final class MSucc[P <: Nat] extends Pos {
  type This = MSucc[P]
  type Add[N <: MInt] = P#Add[N]#Succ
  type Sub[N <: MInt] = Add[N#Neg]
  type AcceptNatVisitor[V <: NatVisitor] = V#VisitSucc[P]
  type Neg = MNeg[This]
  type Pre = P
  type Succ = MSucc[This]
}

final class MNeg[P <: Pos] extends MInt {
  type Add[N <: MInt] = P#Add[N#Neg]#Neg
  type Sub[N <: MInt] = Add[N#Neg]
  type Accept[V <: IntVisitor] = V#VisitNeg[P]
  type Neg = P
  type Succ = P#Pre#Neg
  type Pre = P#Succ#Neg
}