5

は、Scalaでは、測定機能のいくつかのユニットのこの単純化されたスニペットを考えてみましょう:暗黙の検索は、関連のない型パラメータの影響を受けるのはなぜですか?

object UnitsEx { 
    case class Quantity[M <: MInt, T: Numeric](value: T) { 
    private val num = implicitly[Numeric[T]] 
    def *[M2 <: MInt](m: Quantity[M2, T]) = 
     Quantity[M, T](num.times(value, m.value)) 
    } 

    implicit def measure[T: Numeric](v: T): Quantity[_0, T] = Quantity[_0, T](v) 
    implicit def numericToQuantity[T: Numeric](v: T): QuantityConstructor[T] = 
    new QuantityConstructor[T](v) 

    class QuantityConstructor[T: Numeric](v: T) { 
    def m = Quantity[_1, T](v) 
    } 

    sealed trait MInt 
    final class _0 extends MInt 
    final class _1 extends MInt 
} 

このスニペットは、私は現在、取得、使用およびコンパイラのエラーを示しています

import UnitsEx._ 

(1 m) * 1 // Works 
1 * (1 m) // Doesn't work: 
/* 
<console>:1: error: overloaded method value * with alternatives: 
(x: Double)Double <and> 
(x: Float)Float <and> 
(x: Long)Long <and> 
(x: Int)Int <and> 
(x: Char)Int <and> 
(x: Short)Int <and> 
(x: Byte)Int 
cannot be applied to (UnitsEx.Quantity[UnitsEx._1,Int]) 
1 * (1 m) 
^ 
*/ 

measure1をラッピングする修正するだろうしかし、なぜ暗黙のスコープが適用されていないのですか?

私は次のスニペットのように型パラメータMを削除した場合、私はその型パラメータが暗黙の検索に関連しているかを確認することはできませんが、それは、作業を開始:

object UnitsEx2 { 
    case class Quantity[T: Numeric](value: T) { 
    private val num = implicitly[Numeric[T]] 
    def *(m: Quantity[T]) = Quantity[T](num.times(value, m.value)) 
    } 

    implicit def measure[T: Numeric](v: T): Quantity[T] = Quantity[T](v) 
    implicit def numericToQuantity[T: Numeric](v: T): QuantityConstructor[T] = 
    new QuantityConstructor[T](v) 

    class QuantityConstructor[T: Numeric](v: T) { 
    def m = Quantity[T](v) 
    } 
} 

は、この予想されるか、または既知の制限です型チェッカーの?

+0

完全には答えられなかった同様の質問:http://stackoverflow.com/questions/7649517/why-is-the-implicit-conversion-not-considered-in-this-case-with-generic-paramete/ 7650605#7650605 –

+0

おっと...私はその質問に完全に忘れてしまった。 : -/ – soc

+0

それはまだ良い質問です。 –

答えて

1

*演算子の名前を、 multそして1 mult (1 m)はどちらの場合でも機能します。 これはあなたの質問に答えることはできませんが、おそらく重い過負荷の*オペレータに干渉があるという方向を示唆しています。

関連する問題