类型 class 中的多个类型参数
Multiple type parameters in type class
我想用类型class设计转换界面,代码如下:
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)
}
}
但是编译错误:
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]]
如果我把def apply[T:Convert, U:Convert] = implicitly[Convert[T,U]]
改成def apply[T, U](implicit c: Convert[T, U]): Convert[T, U] = c
,没有编译错误!!!
我想知道这是怎么回事?
另外,我查了一些资料,context bound is restricted with single type parameter(?)
如果我想实现多类型参数类型class,我该怎么办?
上下文绑定语法 T : U
仅适用于只有 一个类型参数 S
的类型 U
(符合 T
).
这是有效的,因为您手动声明了 Convert[T, U]
:
的隐式
def covert[T, U](input: T)(implicit c: Convert[T, U]): U = c.convert(input)
以下是无效的,因为编译器将上下文边界去糖化,分别为 Convert[T]
和 Convert[U]
,这没有意义。
def apply[T:Convert, U:Convert] = implicitly[Convert[T,U]]
(尝试脱糖)
def apply[T, U](implicit ev1: Convert[T], ev2: Convert[U]) = ...
参见 SLS 7.4 - Context and View Bounds:
A type parameter A
of a method or non-trait class may also have one or more context bounds 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
. Such evidence consists of an implicit value with type T[S]
.
我想用类型class设计转换界面,代码如下:
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)
}
}
但是编译错误:
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]]
如果我把def apply[T:Convert, U:Convert] = implicitly[Convert[T,U]]
改成def apply[T, U](implicit c: Convert[T, U]): Convert[T, U] = c
,没有编译错误!!!
我想知道这是怎么回事? 另外,我查了一些资料,context bound is restricted with single type parameter(?) 如果我想实现多类型参数类型class,我该怎么办?
上下文绑定语法 T : U
仅适用于只有 一个类型参数 S
的类型 U
(符合 T
).
这是有效的,因为您手动声明了 Convert[T, U]
:
def covert[T, U](input: T)(implicit c: Convert[T, U]): U = c.convert(input)
以下是无效的,因为编译器将上下文边界去糖化,分别为 Convert[T]
和 Convert[U]
,这没有意义。
def apply[T:Convert, U:Convert] = implicitly[Convert[T,U]]
(尝试脱糖)
def apply[T, U](implicit ev1: Convert[T], ev2: Convert[U]) = ...
参见 SLS 7.4 - Context and View Bounds:
A type parameter
A
of a method or non-trait class may also have one or more context boundsA : T
. In this case the type parameter may be instantiated to any typeS
for which evidence exists at the instantiation point thatS
satisfies the boundT
. Such evidence consists of an implicit value with typeT[S]
.