关于call-by-name和0-arity函数之间差异的一些问题

时间:2015-09-26 21:16:03

标签: scala

这里有一些讨论,但我有一些具体的问题,我无法找到答案。因此,通过按名称调用,我的意思是=>T类型,而通过0-arity函数,我的意思是() => T

我理解(我认为)概念上的差异,但可能我错过了一些东西,因为我仍然有很多问题:

  1. 如果我们总能使用=>T,为什么我们会有() => T的概念?
  2. 每个都有任何语法/功能限制吗?现在我发现只有=>不能用作类字段。这是唯一的限制吗?
  3. 两者的生成代码是否始终相同?
  4. 我应该总是更喜欢=>T吗?为什么?
  5. 调用=>T类型是否正确?它在我看来它在scala中没有任何类型表示。

1 个答案:

答案 0 :(得分:7)

1)使用它更方便,特别是在DSL中:

def printAndGet[T](f: => T) = {
  val res = f
  println(res + " printed") 
  res
}

scala> :paste
// Entering paste mode (ctrl-D to finish)

val k = printAndGet {
  val a = 5
  5 * a 
}

// Exiting paste mode, now interpreting.

25 printed
k: Int = 25

2)=> T只能是方法或函数的参数。实际上=> T() => T不可互换:

scala> def aaa(f: => String) = f
aaa: (f: => String)String

scala> val a: Function1[() => String, String] = aaa _
<console>:8: error: type mismatch;
 found   : (=> String) => String
 required: (() => String) => String
       val a: Function1[() => String, String] = aaa _
                                                ^

感谢@ som-snytt,发现这个:

scala> object O { def f(i: Int) = i; def f(i: => Int) = i + 1 }
defined object O

scala> O.f(5)
res12: Int = 5

scala> O.f(5: (=> Int))
<console>:1: error: no by-name parameter type allowed here
       O.f(5: (=> Int))
           ^

即使这个如果编译也应该有用 - 但它没有(scala 2.11.2,2.11.5 REPL只是崩溃):

scala> val k: (=> Int) => Int = O.f _
k: (=> Int) => Int = <function1>

scala> k(5) //should be 6
res18: Int = 5 //WTF?

最后一个似乎是一个错误

3)不完全是,如果你想要相同,只需将=> T转换为() => T

scala> def aaa(f: => String) = {f _}
aaa: (f: => String)() => String

字节码也可能不同。例如,编译器更可能内联=> T的代码而不为其生成lambda。因此,关键的区别在于() => T实际上是一个对象(一等公民),=> T不是。

4)参见1,但有时您可能需要确保用户知道计算可能会延迟 - () => T会更好。

5)它是类型签名的一部分,只需看看eta-expansion:

scala> def aaa(f: => String) = {f}
aaa: (f: => String)String

scala> aaa _ //convert method into a function
res7: (=> String) => String = <function1>

scala> val a: ( => String) =>  String = aaa _
a: (=> String) => String = <function1>

然而,scala不承认它是独立类型:

scala> val a: Function1[( => String), String] = aaa _
<console>:1: error: no by-name parameter type allowed here
       val a: Function1[( => String), String] = aaa _
                          ^
相关问题