11

正如我所理解call-by-name的方法的参数一样,在将相应的参数表达式传递给方法时不会对其进行评估,而只会在(并且如果)在方法体中使用参数的值时才对其进行评估。

然而,在下面的示例中,这仅在前两个方法调用中成立,而在第三个方法调用中不成立,尽管它应该只是第二种情况的语法变体!?

为什么在第三个方法调用中评估参数表达式?

(我使用 Scala 2.11.7 测试了这段代码)

class Node(x: => Int)

class Foo {
  def :: (x: =>Int) = new Node(x)  // a right-associative method
  def !! (x: =>Int) = new Node(x)  // a left-associative method
}

// Infix method call will not evaluate a call-by-name parameter:
val node = (new Foo) !! {println(1); 1}
println("Nothing evaluated up to here")

// Right-associative method call will not evaluate a call-by-name parameter:
val node1 = (new Foo).::({println(1); 1})
println("Nothing evaluated up to here")

// Infix and right-associative method call will evaluate a call-by-name parameter - why??
val node2 = {println(1); 1} ::(new Foo)  // prints 1
println("1 has been evaluated now - why??")

2020 年编辑:请注意,Scala 2.13 不再显示这种恼人的行为:val node2 = ...不再打印任何内容。

4

2 回答 2

10

这是一个错误。一个旧的,在那个。

请参阅SI-1980和 PR #2852

-Xlint链接的拉取请求在使用标志时添加了编译器警告:

<console>:13: warning: by-name parameters will be evaluated eagerly when called as a right-associative infix operator. For more details, see SI-1980.
         def :: (x: =>Int) = new Node(x)  // a right-associative method
             ^
于 2015-11-11T17:52:03.283 回答
3

每当提到它们时,都会评估按名称参数。规范说右关联运算符方法调用是这样评估的:

a op_: b

脱糖:

{ val someFreshName = a; b.op_:(someFreshName) }
//                   ↑↑↑
// Eval happens here ↑↑↑
于 2015-11-11T22:52:26.820 回答