为什么我在这个Scala表达式中需要这个nullary函数的括号?

Ala*_*Dea 3 syntax dsl scala

对于我,这不能用Scala 2.7.7.final或2.8.0.final 编译:

new FileInputStream("test.txt") getChannel transferTo(
    0, Long.MaxValue, new FileOutputStream("test-copy.txt") getChannel)
Run Code Online (Sandbox Code Playgroud)

这对我来说使用Scala 2.7.7.final和2.8.0.final进行编译:

new FileInputStream("test.txt") getChannel() transferTo(
    0, Long.MaxValue, new FileOutputStream("test-copy.txt") getChannel)
Run Code Online (Sandbox Code Playgroud)

为什么我需要做getChannel()而不仅仅是getChannel在这里?

Ken*_*oom 6

一般规则是编译器解释字符串

new FileInputStream("test.txt") getChannel transferTo(...)
Run Code Online (Sandbox Code Playgroud)

object method parameter method parameter method parameter
Run Code Online (Sandbox Code Playgroud)

所以在你的情况下,这意味着

new FileInputStream("test.txt")    // object
getChannel                         // method
transferTo(...)                    // parameter
Run Code Online (Sandbox Code Playgroud)

所以编译器尝试调用transferTo自由函数,因此它可以将其结果作为参数传递给getChannel.当你添加括号时,你会得到

new FileInputStream("test.txt") getChannel() transferTo(...)

new FileInputStream("test.txt")    // object
getChannel                         // method
()                                 // parameter (empty parameter list)
transferTo                         // method
(...)                              // parameter
Run Code Online (Sandbox Code Playgroud)