5

Rozważmy następujący uproszczony fragment niektórych jednostek funkcji pomiarowej w Scala:Dlaczego wyszukiwanie niejawne ma wpływ na niepowiązany parametr typu?

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 
} 

Ten fragment pokazuje zużycie i błąd kompilatora mam obecnie:

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) 
^ 
*/ 

owijając 1 z measure by naprawić problem, ale dlaczego nie ma zastosowania ukryty zakres?

Jeśli usunąć parametr typu M jak w następnym fragmencie to zaczyna działać, chociaż nie widzę, w jaki sposób parametr typu jest związana z niejawnym wyszukiwania:

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) 
    } 
} 

Jest to spodziewane lub znane ograniczenie typu sprawdzającego?

+0

Podobne pytanie, na które nie udzielono pełnej odpowiedzi: http://stackoverflow.com/questions/7649517/why-is-the-implicit-conversion-not-considered-in-this-case-z-generic-paramete/ 7650605 # 7650605 –

+0

Ouch ... Całkowicie zapomniałem, że zadałem to pytanie. : -/ – soc

+0

To wciąż dobre pytanie. –

Odpowiedz

1

Jeśli zmienisz nazwę operatora * na np. mult następnie 1 mult (1 m) działa w obu przypadkach. To nie odpowiada na twoje pytanie, ale sugeruje, że być może jest jakaś ingerencja w mocno przeciążony operator *.

Powiązane problemy