这里有一些讨论,但我有一些具体的问题,我无法找到答案.因此,通过按名称调用,我的意思是=>T类型,我的意思是0-arity函数() => T
我理解(我认为)概念上的差异,但可能我错过了一些东西,因为我仍然有很多问题:
=>T如果我们总能使用,为什么我们有这个概念() => T呢?=>用作类字段.这是唯一的限制吗?=>T吗?为什么?=>T类型是否正确?它看起来像它在scala中没有任何类型表示.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
Run Code Online (Sandbox Code Playgroud)
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 _
^
Run Code Online (Sandbox Code Playgroud)
感谢@ 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))
^
Run Code Online (Sandbox Code Playgroud)
即使这个应该工作,如果它编译 - 但它没有(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?
Run Code Online (Sandbox Code Playgroud)
最后一个看起来像个bug
3)不完全是,如果你想要相同,只需转换=> T成() => T:
scala> def aaa(f: => String) = {f _}
aaa: (f: => String)() => String
Run Code Online (Sandbox Code Playgroud)
字节码也可能不同.例如,编译器更可能内联代码=> T而不为其生成lambda.所以,关键的区别在于它() => T实际上是一个对象(一等公民),=> T而不是.
4)参见1,但有时您可能需要确保用户知道计算可能会延迟 - () => T那就更好了.
5)它是类型签名的一部分,只需看看以下的eta扩展:
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>
Run Code Online (Sandbox Code Playgroud)
但是scala不承认它是独立类型:
scala> val a: Function1[( => String), String] = aaa _
<console>:1: error: no by-name parameter type allowed here
val a: Function1[( => String), String] = aaa _
^
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
436 次 |
| 最近记录: |