Scala compiler not recognizing a view bound

前端 未结 2 1200
太阳男子
太阳男子 2021-01-07 02:01

I\'ve tried this line of code

def **[A <% Numeric[A]](l:List[A],m:List[A])=l.zip(m).map({t=>t._1*t._2})

However on compilation, I get

2条回答
  •  失恋的感觉
    2021-01-07 02:36

    You can also solve this with a context bound. Using the context method from this answer, you can write:

    def **[A : Numeric](l:List[A],m:List[A]) =
       l zip m map { t => context[A]().times(t._1, t._2) }
    

    or

    def **[A : Numeric](l:List[A],m:List[A]) = {
       val num = context[A]()
       import num._
       l zip m map { t => t._1 * t._2 }
    }
    

提交回复
热议问题