Why is the implicit conversion not considered in this case with generic parameters?

前端 未结 1 2002
轻奢々
轻奢々 2021-01-20 18:58

Consider the following code, derived from the metascala project:

object Units {
  case class Quantity[M <: MInt, T: Numeric](value: T) {
    type This = Q         


        
相关标签:
1条回答
  • 2021-01-20 19:23

    To get things started, here's a stand-alone example that gives the same problem,

    object Units {
      case class Quantity[T: Numeric](value: T) {
        def *[M](m: Quantity[T]) =           // type M can't be inferred below
          Quantity[T](implicitly[Numeric[T]].times(value, m.value))
      }
      implicit def measure[T: Numeric](v: T) = Quantity[T](v)
    
      val length0 = measure(5) * Quantity(5) // works
      val length1 = 5  * Quantity(5)         // doesn't work
    }
    

    For some reason, the conversion measure isn't being found because of the type parameter M on the method *. If the type parameter is removed from *, things compile fine. Maybe someone else can explain why?

    Edit. This is looking like a limitation of the Scala compiler, since renaming * to something like *** resolves the problem. Perhaps the existence of Int.* (without a type parameter) is precluding the implicit conversion for use of Quantity.*[M] (with a type parameter). This reminds me of the requirement that overridden methods must have the same exact type parameters.

    0 讨论(0)
提交回复
热议问题