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

Scala中的波浪号操作符

发布时间:2020-12-16 09:44:01 所属栏目:安全 来源:网络整理
导读:在这一点Scala中做什么? 例如: scala val apple = 1apple: Int = 1scala ~appleres0: Int = -2 那个蠕虫对我的苹果有什么影响? 解决方法 首先,一些元建议。任何时候您都想知道编译器如何扩展一些语法糖,推断类型或应用隐式转换,请使用scala -Xprint:t
在这一点Scala中做什么?

例如:

scala> val apple = 1
apple: Int = 1

scala> ~apple
res0: Int = -2

那个蠕虫对我的苹果有什么影响?

解决方法

首先,一些元建议。任何时候您都想知道编译器如何扩展一些语法糖,推断类型或应用隐式转换,请使用scala -Xprint:typer -e< expr>告诉你发生了什么。

scala -Xprint:typer -e "val a = 2; ~a"

...
private[this] val a: Int = 2;
private <stable> <accessor> def a: Int = $anon.this.a;
$anon.this.a.unary_~

好的,一个前缀?扩展到一个正则方法调用unary_?。

从language specification:

6.12.1 Pre?x Operations

A pre?x operation op e consists of a pre?x operator op,which must be one of the identi?ers +,-,! or ~.
The expression op e is equivalent to the post?x method application
e.unary_op.

Pre?x operators are different from normal function applications in
that their operand expression need not be atomic. For instance,the
input sequence -sin(x) is read as -(sin(x)),whereas the function
application negate sin(x) would be parsed as the application of the
in?x operator sin to the operands negate and (x).

这意味着前缀运算符不限于内置类型,它们可以在您自己的类型中使用(尽管使用此功能不是很疯狂)

scala> object foo { def unary_~ = "!!!" }
defined module foo

scala> ~foo
res0: java.lang.String = !!!

那么你的问题是什么?您可以为methods starting with u的标准库检出ScalaDoc的索引。nightly ScalaDoc有一些最近添加的此方法的文档。

the bitwise negation of this value
Example:
~5 == -6
// in binary: ~00000101 ==
//             22222010

(编辑:李大同)

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

    推荐文章
      热点阅读