Scala - Parentheses and Curly Brackets in Anonymous Functions
来源:互联网 发布:淘宝势力周多久一次 编辑:程序博客网 时间:2024/04/30 11:29
Why lst map {c:Char => 1} and lst map ((c:Char) => 1) work fine, but lst map (c:Char => 1) gives us compilation error:
error: identifier expected but integer literal found.
lst map (c:Char => 1)
To answer this question we should look into Scala Language Specification, into part 6.23 Anonymous Functions.There is a description how anonymous function can be defined:
Expr ::= (Bindings | [‘implicit’] id | ‘_’) ‘=>’ ExprResultExpr ::= (Bindings | ([‘implicit’] id | ‘_’) ‘:’ CompoundType) ‘=>’ BlockBindings ::= ‘(’ Binding {‘,’ Binding} ‘)’Binding ::= (id | ‘_’) [‘:’ Type]
As you can see Bindings requires to be placed inside two surrounding parentheses. So if anonymous function defines the type of the parameter, as in our example, it has to be defined in this way:
(c:Char)=>1
And the call to map should look like that:
lst map((c:Char)=>1)
So if anonymous function is defied as last expression in a code block, and has exacly one parameter, you can use abbreviated syntax, without parenthesis aroundc:Char, to define anonymous function. So, in our example, we can write c:Char => 1, but only when we place it inside a code block {c:Char => 1}. And we can call map function this way:
lst map({c:Char=>1})
or with abbreviated syntax without parenthesis:
lst map {c:Char=>1}
This is summarized in changelog at the end of the documentation (Changes in Version 2.1.7 (19-Jul-2006)):
The syntax of closures has been slightly restricted (§6.23). The form
x: T => E
is valid only when enclosed in braces, i.e. { x: T => E }. The following is illegal, because it might be read as the value x typed with the type T => E:
val f = x: T => E
Legal alternatives are:
val f = { x: T => E }
val f = (x: T) => E
Another way to specify anonymous functions:
If we look closer at specification we can see that it allows us to use another way to define anonymous function:
Expr ::= (Bindings | [‘implicit’] id | ‘_’) ‘=>’ Expr
We can see that we can define your anonymous function without parameter binding in those two ways (if we don't need to specify argument type):
lst map (c =>1)// orlst map (_=>1)
I hope that this article clarified how we can declare anonymous functions and it shouldn't cause a confusion any more.
-------------------------------------------------------
In general, there are many cases when you would prefer curly braces (e.g. multiline expressions, for comprehensions), but let's talk specifically about
when it's written on a single line, is there any inherent reason to use one over the other
In a second case it's not just curly braces, instead of parentheses, it's curly braces with ommited parentheses. Scala allows you to ommit parenthesis sometimes, and the later syntax is used to access to the niceties you got in partial functions (namely, pattern matching), so
lst foreach {x => println(s"you get the idea, $x")}
is actually
lst foreach({x => println(s"you get the idea, $x")})
which, as I said, can be useful from pattern matching POV:
val map = Map("foo" -> "bar")map foreach { case (k, v) => println(s"key: $k, value: $v") }// which is not possible with the usual parenthesis
- Scala - Parentheses and Curly Brackets in Anonymous Functions
- Closure and anonymous functions in Objective-C
- Define Functions in Scala using Anonymous Inner Class
- Prototype.js, onComplete and anonymous functions
- Anonymous types in C# (and .NET 3.5)
- Threads and Anonymous Classes in JAVA
- Lambda- anonymous functions
- Javascript anonymous functions
- self-Executing Anonymous Functions
- Scala新手指南中文版 - 第四篇 Pattern Matching Anonymous Functions(模式匹配匿名函数)
- Scala新手指南中文版 - 第四篇 Pattern Matching Anonymous Functions(模式匹配匿名函数)
- php_lang_ref:Language Reference >> Functions >> Anonymous functions
- Substitute and Replace functions in PeopleCode
- The implementation of anonymous methods in C# and its consequences
- Scala-3 - 5 - Lecture 2.5 - Functions and Data (11_50)
- Scala函数式程序设计 week3 Functions and State
- Classes and Fields in Scala
- Anonymous function in C++
- 树莓派一些常用设置
- Chromium网页渲染调度器(Scheduler)实现分析
- Android 网络与数据存储
- 搭建开发环境
- 心得
- Scala - Parentheses and Curly Brackets in Anonymous Functions
- Ubuntu安装攻略
- Hibernate4学习总结(3)--注解形式的基础映射,主键映射,基本属性映射,复合属性映射,继承映射。
- OC阅读笔记十一:引用计数
- DBL_EPSILON和 FLT_EPSILON的用法
- HDUOJ_1998_奇数阶魔方
- 死锁
- Geekband005第五周笔记分享
- 多线程 以及多线程安全