2011-10-21 5 views
6

Units.scala 파일에 정의 된 metascala의 측정 단위 기능으로 작업하는 동안 문제가 발생했습니다.유형 수준 계산을 사용하는 동안 유형 유추/유형 확인에 실패했습니다.

이 질문의 나머지 부분에 대해서는 단 하나의 단위 유형 인 길이의 단순화 된 체계를 사용합니다.

Quantity[_1] 
     ^
      | 
     Length 

는 즉시 유형을 추론 할 필요가 같은 문제가 시작이 문제를 보여주는 충분한 것
Quantity[_1, _0, _0, _0, _0, _0, _0] 
     ^^^^^^^
      | | | | | | | 
      | Mass | Crncy.| Mol | 
     Length Time Temp. Lum.Intensity 

처럼

그래서 현실의 유형은 보이는 곳.

(또한 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]과 동일하다는 것을 알아 내십시오. 지. 지역에서가는 (1)처럼 DIST합니다 :

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

혼란스러운 것은 전자 "치수를 추가 할"때 작동합니다. 지. (2)에서 같은 지역에 길이에서가는 :.

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

(여기에 전체 코드를 붙여하지 죄송합니다, 그것은 너무 그냥 내 예를 최소화하기 위해 노력하지만 실패 난 그냥 연결하고 있습니다 이유입니다.)

답변

2

유형 SubSubtractableMInt 형질에 없습니다. 간단한 정의를 사용하면 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 repo에 대한 두 가지 수정 사항을 커밋했습니다. –