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

scala – 当我混合Implicits时,编译错误,输入参数和Nothing

发布时间:2020-12-16 18:38:51 所属栏目:安全 来源:网络整理
导读:为什么不能使用类型参数进行编译: error: value explode is not a member of Test.A[Nothing] 如果我删除它们,那么它编译.我不理解什么,更重要的是,我能做些什么来解决它. object Test extends App { implicit class B[E](set: A[E]) { def explode() = pri
为什么不能使用类型参数进行编译:

error: value explode is not a member of Test.A[Nothing]

如果我删除它们,那么它编译.我不理解什么,更重要的是,我能做些什么来解决它.

object Test extends App {

  implicit class B[E](set: A[E]) {
    def explode() = println("boom")
  }

  case class A[E](name: String)

  A[Nothing]("coldplay").explode()
}

(此示例中的类型参数不执行任何操作,但在实际情况下,我有多个类型参数,有些可能是Nothing而有些则不是).

解决方法

它真的不喜欢推断:

scala> implicit def a2b(a: A[Nothing]): B[Nothing] = new B(a)
<console>:17: error: type mismatch;
 found   : A[Nothing]
 required: A[T]
Note: Nothing <: T,but class A is invariant in type E.
You may wish to define E as +E instead. (SLS 4.5)
       implicit def a2b(a: A[Nothing]): B[Nothing] = new B(a)
                                                           ^

scala> implicit def a2b(a: A[Nothing]): B[Nothing] = new B[Nothing](a)
warning: there were 1 feature warning(s); re-run with -feature for details
a2b: (a: A[Nothing])B[Nothing]

scala> A[Nothing]("coldplay").explode()
boom

-Ytyper-debug:

|    |    |    |    |    |    |    solving for (T: ?T)
|    |    |    |    |    |    |    |-- $iw.this.X.B BYVALmode-EXPRmode-FUNmode-POLYmode (silent solving: type T: method f in X) implicits disabled
|    |    |    |    |    |    |    |    [adapt] [T](set: $line3.$read.$iw.$iw.X.A[T])$line3.$read.$iw.$iw... adapted to [T](set: $line3.$read.$iw.$iw.X.A[T])$line3.$read.$iw.$iw...
|    |    |    |    |    |    |    |    -> (set: X.A[T])X.B[T]
|    |    |    |    |    |    |    solving for (T: ?T,T: ?T)
|    |    |    |    |    |    |    -> <error>
<console>:10: error: value explode is not a member of X.A[Nothing]
       def f() = A[Nothing]("coldplay").explode() }

(编辑:李大同)

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

    推荐文章
      热点阅读