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

scala abusive隐式转换

发布时间:2020-12-16 18:12:11 所属栏目:安全 来源:网络整理
导读:在 scala 2.10 REPFL中: class E(val i: Int) { def += (other: E) = i - other.i }implicit def toE(i: Int) = new E(i)var j = 1j += 3 结果是: res1: Int = -2 jj: Int = 1 请注意,与使用先前的转换方法相比,如果E类本身被标记为隐式,则不会干扰regular
在 scala 2.10 REPFL中:

>
class E(val i: Int) { def += (other: E) = i - other.i }
implicit def toE(i: Int) = new E(i)
var j = 1
j += 3

结果是:

res1: Int = -2
> j
j: Int = 1

请注意,与使用先前的转换方法相比,如果E类本身被标记为隐式,则不会干扰regular =.

哇,如果这个隐式转换在范围内,我已经能够完全破坏程序!如果已经存在意义,有没有办法让scala不转换为implicits?

解决方法

如果可以更改此行为,我无法回答,但我在规范中找到了以下描述(第85页):

Assignment operators are treated specially in that they can be
expanded to assignments if no other interpretation is valid. Let’s
consider an assignment operator such as += in an in?x operation l +=
r,where l,r are expressions. This operation can be re-interpreted as
an operation which corresponds to the assignment l = l + r

据我所知,因为你提供了另一种解释,所以不会进行扩展. Myabe将有助于追踪问题.

(编辑:李大同)

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

    推荐文章
      热点阅读