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?
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 –
Ouch ... Całkowicie zapomniałem, że zadałem to pytanie. : -/ – soc
To wciąż dobre pytanie. –