简体   繁体   English

类型中的多个类型参数

[英]Multiple type parameters in type class

I want to use type class to design convert interface and codes are as below: 我想使用类型类来设计转换接口,代码如下:

case class Kilograms(value: Double)

case class Pounds(value: Double)

trait Convert[T, U] {
  def convert(input: T): U
}

object Convert {
  def apply[T:Convert, U:Convert] = implicitly[Convert[T,U]]
  def covert[T, U](input: T)(implicit c: Convert[T, U]): U = c.convert(input)

  implicit object kilogramsToPounds extends Convert[Kilograms, Pounds] {
      override def convert(input: Kilograms): Pounds = Pounds(input.value *   2.20462)
    }

  implicit object poundsToKilograms extends Convert[Pounds, Kilograms] {
      override def convert(input: Pounds): Kilograms = Kilograms(input.value / 2.20462)
    }
}

But compile error: 但编译错误:

Error: wrong number of type arguments for A$A95.this.Convert, should be 2
 def apply[T:Convert, U:Convert] = implicitly[Convert[T,U]]

 Error: could not find implicit value for parameter e: A$A95.this.Convert[T,U]
  def apply[T:Convert, U:Convert] = implicitly[Convert[T,U]]

Error: not enough arguments for method implicitly: (implicit e:   A$A95.this.Convert[T,U])A$A95.this.Convert[T,U].
Unspecified value parameter e.
   def apply[T:Convert, U:Convert] = implicitly[Convert[T,U]]

If I change def apply[T:Convert, U:Convert] = implicitly[Convert[T,U]] into def apply[T, U](implicit c: Convert[T, U]): Convert[T, U] = c , no compile error!!! 如果我改变def apply[T:Convert, U:Convert] = implicitly[Convert[T,U]]def apply[T, U](implicit c: Convert[T, U]): Convert[T, U] = c ,没有编译错误!

I would like to know what's going on? 我想知道发生了什么事? Also, I look up some information, context bound is restricted with single type parameter(?) If I want to implement multiple type parameter type class, how should I do? 另外,我查了一些信息,上下文绑定是用单一类型参数限制的(?)如果我想实现多个类型参数类型类,我该怎么办?

The context-bound syntax T : U only works for types U that have only one type parameter S (which conforms to T ). 上下文绑定语法T : U仅适用于只有一个类型参数 S (符合T )的类型U

This is valid because you are manually declaring the implicit for Convert[T, U] : 这是有效的,因为您手动声明了Convert[T, U]的隐式:

def covert[T, U](input: T)(implicit c: Convert[T, U]): U = c.convert(input)

The following is not valid, because the compiler de-sugars the context bounds to Convert[T] and Convert[U] respectively, which doesn't make sense. 以下内容无效,因为编译器分别对Convert[T]Convert[U]的上下文边界进行去糖,这没有意义。

 def apply[T:Convert, U:Convert] = implicitly[Convert[T,U]]

(attempts to de-sugar to) (尝试脱糖)

 def apply[T, U](implicit ev1: Convert[T], ev2: Convert[U]) = ...

See the SLS 7.4 - Context and View Bounds : 请参阅SLS 7.4 - 上下文和视图边界

A type parameter A of a method or non-trait class may also have one or more context bounds A : T . 方法或非特征类的类型参数A也可以具有一个或多个上下文边界A : T In this case the type parameter may be instantiated to any type S for which evidence exists at the instantiation point that S satisfies the bound T . 在这种情况下,类型参数可以被实例化为任何类型S对于该类型S ,证据存在于S满足绑定T的实例化点处。 Such evidence consists of an implicit value with type T[S] . 这些证据由类型为T[S]的隐含值组成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM