加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 类型类中的多个类型参数

发布时间:2020-12-16 18:08:44 所属栏目:安全 来源:网络整理
导读:我想使用类型类来设计转换接口,代码如下: 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[
我想使用类型类来设计转换接口,代码如下:

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]]

 Error: could not find implicit value for parameter e: A$A95.this.Convert[T,U]
  def apply[T:Convert,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]]

如果我改变def apply [T:Convert,U:Convert] =隐式[Convert [T,U]]为def apply [T,U](隐式c:转换[T,U]):转换[T,U] = c,没有编译错误!

我想知道发生了什么事?
另外,我查找一些信息,上下文限制是用单一类型参数(?)限制的
如果我想实现多个类型参数类型类,我该怎么办?

解决方法

上下文绑定语法T:U仅适用于只有一个类型参数S(符合T)的类型U.

这是有效的,因为您手动声明了Convert [T,U]的隐式:

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

以下内容无效,因为编译器分别对Convert [T]和Convert [U]的上下文边界进行去糖,这没有意义.

def apply[T:Convert,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].

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读