如何使用Scala 2.10隐式类
发布时间:2020-12-16 09:19:28 所属栏目:安全 来源:网络整理
导读:我认为这将正确使用 Scala 2.10的新隐式类: implicit case class IntOps(i: Int) extends AnyVal { def twice = i * 2}11.twice 显然不是: console:13: error: value twice is not a member of Int 11.twice ^ 我错过了什么(Scala 2.10.0-M6)吗? 解决方法
我认为这将正确使用
Scala 2.10的新隐式类:
implicit case class IntOps(i: Int) extends AnyVal { def twice = i * 2 } 11.twice 显然不是: <console>:13: error: value twice is not a member of Int 11.twice ^ 我错过了什么(Scala 2.10.0-M6)吗? 解决方法
一个线索是隐晦的课程,在
the SIP-13年解释:
implicit class RichInt(n: Int) extends Ordered[Int] { def min(m: Int): Int = if (n <= m) n else m ... } 将由编译器转换如下: class RichInt(n: Int) extends Ordered[Int] { def min(m: Int): Int = if (n <= m) n else m ... } implicit final def RichInt(n: Int): RichInt = new RichInt(n) 如您所见,创建的隐式函数与原始类具有相同的名称.我想这样做是为了使隐式类更容易导入. 因此,在您的情况下,当您创建一个隐式案例类时,由隐式关键字创建的方法名称与由case关键字创建的协同对象之间存在冲突. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |