网上找的一篇关于 By-name-parameter 和 Function区别,虽然是2009年的文章了,但解释的非常好。 原来By-name-parameter和函数类型是不一样的概念,以前还以为By-name-paramete 就是函数类型的一个用法呢
By-name-parameter to Function
Today's topic is related to Defining Custom Control Structures. By name parameters are not function objects in the same way as other functions. Normally you need to invoke a function:
Compare that to a by-name-parameter function:
So the issue is how can you pass a by-name-parameter to a method that takes a function as a parameter without having to do:
the alternative syntax is:
- scala> def exec(func: () => Int) = func()
- exec: (() => Int)Int
- // exec invokes the function so 10 is the result
- scala> exec (() => 10)
- res1: Int = 10
- scala> def exec(func: () => Int) = func
- exec: (() => Int)() => Int
- // here exec returns the function:
- scala> exec (() => 10)
- res2: () => Int = <function>
Compare that to a by-name-parameter function:
- scala> def exec(func: => Int) = func
- exec: (=> Int)Int
- // by-name-parameters are executed when they are referenced
- // so the result is 10
- scala> exec {10}
- res3: Int = 10
- // This is not legal because by-name-parameters
- // are not normal functions
- scala> def exec(func: => Int) = func()
- <console>:4: error: func of type Int does not take parameters
- def exec(func: => Int) = func()
So the issue is how can you pass a by-name-parameter to a method that takes a function as a parameter without having to do:
- scala> def takesFunc(func: () => Int) = println(func())
- takesFunc: (() => Int)Unit
- scala> def send(x: => Int) = takesFunc(() => x)
- send: (=> Int)Unit
- scala> send{2}
- 2
the alternative syntax is:
- scala> def send(x: => Int) = takesFunc (x _)
- send: (=> Int)Unit
- scala> send {2}
- 2
参考:http://daily-scala.blogspot.sg/2009/12/by-name-parameter-to-function.html
相关内容参考:Scala“call by name“和 “call by value” 比较
相关推荐
赠送jar包:scala-parser-combinators_2.11-1.0.4.jar; 赠送原API文档:scala-parser-combinators_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-parser-combinators_2.11-1.0.4-sources.jar; 赠送Maven依赖信息...
赠送jar包:scala-parser-combinators_2.12-1.1.0.jar; 赠送原API文档:scala-parser-combinators_2.12-1.1.0-javadoc.jar; 赠送源代码:scala-parser-combinators_2.12-1.1.0-sources.jar; 赠送Maven依赖信息...
赠送jar包:scala-compiler-2.11.8.jar; 赠送原API文档:scala-compiler-2.11.8-javadoc.jar; 赠送源代码:scala-compiler-2.11.8-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.8.pom; 包含翻译后...
1. **Scala编译器和运行时库**:这是开发Scala项目所必需的基础组件,包括Scala编译器(scalac)和Scala标准库。 2. **插件本身**:包含用于在IntelliJ IDEA中集成Scala开发环境的代码和配置文件。这些文件使得IDE...
赠送jar包:scala-parser-combinators_2.11-1.0.4.jar; 赠送原API文档:scala-parser-combinators_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-parser-combinators_2.11-1.0.4-sources.jar; 包含翻译后的API...
赠送jar包:scala-java8-compat_2.11-0.7.0.jar; 赠送原API文档:scala-java8-compat_2.11-0.7.0-javadoc.jar; 赠送源代码:scala-java8-compat_2.11-0.7.0-sources.jar; 赠送Maven依赖信息文件:scala-java8-...
赠送jar包:scala-compiler-2.11.12.jar; 赠送原API文档:scala-compiler-2.11.12-javadoc.jar; 赠送源代码:scala-compiler-2.11.12-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.12.pom; 包含...
赠送jar包:scala-java8-compat_2.11-0.7.0.jar; 赠送原API文档:scala-java8-compat_2.11-0.7.0-javadoc.jar; 赠送源代码:scala-java8-compat_2.11-0.7.0-sources.jar; 赠送Maven依赖信息文件:scala-java8-...
scala eclipse插件.对应scala版本:2.10--2.11,对应eclipes版本:4.4--4.5. update site:http://download.scala-ide.org/sdk/lithium/e44/scala211/stable/site 下载地址:...
Scala是一种强大的多范式编程语言,它融合了函数式编程和面向对象编程的特点。IntelliJ IDEA是一款广受赞誉的Java开发集成环境,为开发者提供了高效、智能的代码编写体验。"scala-intellij-bin-0.41"是专门为...
scala-intellij-bin-2018.3.2.zip插件,亲测可用!!!scala-intellij-bin-2018.3.2.zip插件,亲测可用!!!scala-intellij-bin-2018.3.2.zip插件,亲测可用!!!
赠送jar包:scala-library-2.11.8.jar; 赠送原API文档:scala-library-2.11.8-javadoc.jar; 赠送源代码:scala-library-2.11.8-sources.jar; 赠送Maven依赖信息文件:scala-library-2.11.8.pom; 包含翻译后的API...
赠送jar包:scala-compiler-2.12.7.jar; 赠送原API文档:scala-compiler-2.12.7-javadoc.jar; 赠送源代码:scala-compiler-2.12.7-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.12.7.pom; 包含翻译后...
赠送jar包:scala-reflect-2.11.8.jar; 赠送原API文档:scala-reflect-2.11.8-javadoc.jar; 赠送源代码:scala-reflect-2.11.8-sources.jar; 赠送Maven依赖信息文件:scala-reflect-2.11.8.pom; 包含翻译后的API...
scala-SDK-4.7.0-vfinal-2.12-linux.gtk.x86_64.tar.gz scala-SDK-4.7.0-vfinal-2.12-linux.gtk.x86_64.tar.gz
赠送jar包:scala-compiler-2.11.0.jar; 赠送原API文档:scala-compiler-2.11.0-javadoc.jar; 赠送源代码:scala-compiler-2.11.0-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.0.pom; 包含翻译后...
Scala是一种强大的多范式编程语言,它融合了面向对象和函数式编程的特性。IntelliJ IDEA是一款广受欢迎的集成开发环境(IDE),尤其在Java和Scala开发者中有着极高的赞誉。"scala-intellij-bin-2021.3.6.zip"是一个...
赠送jar包:scala-xml_2.12-1.0.6.jar; 赠送原API文档:scala-xml_2.12-1.0.6-javadoc.jar; 赠送源代码:scala-xml_2.12-1.0.6-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.12-1.0.6.pom; 包含翻译后的API...
Scala是一种强大的多范式编程语言,它融合了函数式编程和面向对象编程的特点。IntelliJ IDEA是一款广受赞誉的Java集成开发环境(IDE),它为开发者提供了丰富的工具和功能来提升开发效率。针对Scala语言,IntelliJ ...
赠送jar包:scala-xml_2.11-1.0.4.jar; 赠送原API文档:scala-xml_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.4-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.11-1.0.4.pom; 包含翻译后的API...