2011-10-21 26 views
6

我在使用文件Units.scala中定義的metascala中的測量單位功能進行工作時遇到了問題。使用類型級計算時輸入推理/類型檢查失敗

對於這個問題的其餘部分,我將使用一個簡化的方案,只有一個單位類型,長度。

那麼,在現實中一種看起來像

Quantity[_1, _0, _0, _0, _0, _0, _0] 
     ^^^^^^^
      | | | | | | | 
      | Mass | Crncy.| Mol | 
     Length Time Temp. Lum.Intensity 

這將足以證明問題:

Quantity[_1] 
     ^
      | 
     Length 

只要型需要進行推斷,麻煩開始。

考慮這個例子(也有看代碼UnitsTest.scala):

val length: Quantity[_1] = m(5) 
val area: Quantity[_2] = length * length // (1) Works 
val dist: Quantity[_1] = area/length // (2) Doesn't work! 

我得到一個錯誤在最後一行說:

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[_1]。 G。從區域去DIST像(1)

Quantity[_2 - _1] <<not equal to>> Quantity[_1] 

混亂的事情是,它在「增加一個層面」電子工程。 G。在(2)從長打算面積,如:。

Quantity[_1 + _1] <<equal to>> Quantity[_2] 

(對不起,這裏沒有粘貼整個代碼,它只是太多我竭力把我的例子,但我沒這就是爲什麼我只是鏈接。 )

回答

2

類型SubSubtractable缺少MInt特徵。如果要減去MSuccMNeg中的某個類型,則使其起作用的一個簡單定義是執行否定添加。

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 
} 

還有一兩件事,在/方法Quantity應劃分其參數,而不是乘他們!

+0

是的,這是修復。我已經向MetaScala回購提交了兩個修復程序。 –