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

scala – 结合使用名称冒号操作符和空间不一致

发布时间:2020-12-16 08:58:36 所属栏目:安全 来源:网络整理
导读:参见英文答案 Why does Scala evaluate the argument for a call-by-name parameter if the method is infix and right-associative?????????????????????????????????????2个 这是以下问题.当我运行以下内容时: object Test { def /:(s: = Unit) = { print
参见英文答案 > Why does Scala evaluate the argument for a call-by-name parameter if the method is infix and right-associative?????????????????????????????????????2个
这是以下问题.当我运行以下内容时:

object  Test {
  def /:(s: => Unit) = {
    println("/:")
    s
  }
}

println("A") /: Test

它打印:

A
/:

但是,我期待它打印:

/:
A

因为最后一个表达式被认为是重写的Test./:(println(”A“)) – 顺便提一下,给出了第二个值.

有没有人知道使第一种语法有效的方法,例如println(“A”)/:测试但是有名字叫号?

编辑

使用desugar方法,我发现调用的方式不同.

> desugar { println("A") /: Test}
 val x$1: Unit = println("A");
 Test./:(x$1)

因此我仍然想知道为什么这个选择.

解决方法

这是一个 known issue.
如果使用-Xlint选项进行编译,则应该看到 warning.

$scalac -Xlint temp.scala
temp.scala:2: warning: by-name parameters will be evaluated eagerly when called 
              as a right-associative infix operator. For more details,see SI-1980.
    def /:(s: => Unit) = {
        ^
one warning found

(编辑:李大同)

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

    推荐文章
      热点阅读