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

如何在Scala中修复此类型不匹配错误?

发布时间:2020-12-16 18:20:53 所属栏目:安全 来源:网络整理
导读:我在 Scala REPL中收到以下错误: scala trait Foo[T] { def foo[T]:T }defined trait Fooscala object FooInt extends Foo[Int] { def foo[Int] = 0 }console:8: error: type mismatch;found : scala.Int(0)required: Int object FooInt extends Foo[Int] {
我在 Scala REPL中收到以下错误:

scala> trait Foo[T] { def foo[T]:T  }
defined trait Foo

scala> object FooInt extends Foo[Int] { def foo[Int] = 0 }
<console>:8: error: type mismatch;
found   : scala.Int(0)
required: Int
   object FooInt extends Foo[Int] { def foo[Int] = 0 }
                                                   ^

我想知道它究竟意味着什么,以及如何解决它.

解决方法

您可能在方法foo上不需要该类型参数.问题是它影响了它的特性Foo的类型参数,但它不一样.

object FooInt extends Foo[Int] { 
     def foo[Int] = 0 
          //  ^ This is a type parameter named Int,not Int the class.
 }

同样,

trait Foo[T] { def foo[T]: T  }
           ^ not the    ^
              same T

你应该删除它:

trait Foo[T] { def foo: T  }
 object FooInt extends Foo[Int] { def foo = 0 }

(编辑:李大同)

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

    推荐文章
      热点阅读