如果在调用按值时
val f: (Int) => Int = {(i) => {i * i}} # f: Int => Int = <function1>
是简写
val f: Function1[Int, Int] = {(i) => {i * i}} # f: Int => Int = <function1>
然后调用按名称
val f: (=> Int) => Int = {(i) => {i * i}} # f: (=> Int) => Int = <function1>
是简写
的?什么?
以及
调用按值时
val f = {(i) => {i * i}}:(Int) => Int # f: Int => Int = <function1>
是简写
val f = {(i) => {i * i}}:Function1[Int, Int] # f: Int => Int = <function1>
然后调用按名称
val f = {(i) => {i * i}}:(=>Int) => Int # f: (=> Int) => Int = <function1>
是简写
的?什么?
换句话说
如果(Int)=&gt; Int 是 Function1 [Int,Int]
的简写然后(=&gt; Int)=&gt; Int 是的简写?什么?
谢谢!
答案 0 :(得分:1)
在字节码级别,它是:
的简写Function1[Function0[Int], Int]
如果要从其他JVM语言调用此类Scala代码,则必须填写该签名。
请参阅the source code for Function0,您在scaladoc中找不到它
答案 1 :(得分:1)
这不是任何简写。按名称类型是按名称类型。见SLS 4.6.1,http://www.scala-lang.org/files/archive/spec/2.11/04-basic-declarations-and-definitions.html#by-name-parameters。
确实,如果查看生成的字节码,您将看到该参数将作为Function0
传递,但这是字节码级实现细节。在语言层面,名字类型不仅仅是语法糖。它们是实际类型(尽管它们只能作为参数类型出现,而不是在其他上下文中出现)。