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

定义我自己的toInt方法时scala中的异常

发布时间:2020-12-16 19:15:08 所属栏目:安全 来源:网络整理
导读:为什么这段代码会抛出异常? val x = new { def toInt(n: Int) = n*2 }x.toInt(2)scala.tools.nsc.symtab.Types$TypeError: too many arguments for method toInteger: (x$1: java.lang.Object)java.lang.Integer at scala.tools.nsc.typechecker.Contexts$C
为什么这段代码会抛出异常?

val x = new { def toInt(n: Int) = n*2 }
x.toInt(2)
scala.tools.nsc.symtab.Types$TypeError: too many arguments for method toInteger: (x$1: java.lang.Object)java.lang.Integer
        at scala.tools.nsc.typechecker.Contexts$Context.error(Contexts.scala:298)
        at scala.tools.nsc.typechecker.Infer$Inferencer.error(Infer.scala:207)
        at scala.tools.nsc.typechecker.Infer$Inferencer.errorTree(Infer.scala:211)
        at scala.tools.nsc.typechecker.Typers$Typer.tryNamesDefaults$1(Typers.scala:2350)
        ...

我正在使用scala 2.9.1.final

解决方法

显然是一个编译器错误(编译器崩溃,REPL告诉你该条目似乎已经杀死了编译器.).它没有表明您的代码有任何问题.

你正在创建一个AnyRef类型的单个实例{def toInt(n:Int):Int},所以创建一个像Kyle所建议的单例对象可能是一种更好的方法.或者创建一个您实例化的命名类/特征,它可以正常工作.

(编辑:李大同)

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

    推荐文章
      热点阅读