• 为了保证你在浏览本网站时有着更好的体验，建议使用类似Chrome、Firefox之类的浏览器~~
• 如果你喜欢本站的内容何不Ctrl+D收藏一下呢，与大家一起分享各种编程知识~
• 本网站研究机器学习、计算机视觉、模式识别~当然不局限于此，生命在于折腾，何不年轻时多折腾一下

# scala语法糖之括号论

3个月前 (01-04) 210次浏览

scala> var add = (x: Int,y: Int) => x + y
add: (Int, Int) => Int =

res0: Int = 3

:1: error: ‘;’ expected but ‘,’ found.
^

scala> var increase = (x: Int) => x + 1
increase: Int => Int =

scala> increase(10)
res0: Int = 11

scala> increase{10}
res1: Int = 11

scala> def method(x: Int) = {
| x + 1
| }
method: (x: Int)Int

scala> method {
| 1 +
| 2
| 3
| }
:14: warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses
1 +
^
res14: Int = 4

scala> method(
| 1 +
| 2
| 3
:4: error: ‘)’ expected but integer literal found.
3
^

scala> val tupleList = List[(String, String)]()
tupleList: List[(String, String)] = List()

scala> val filtered = tupleList.takeWhile( case (s1, s2) => s1 == s2 )
:1: error: illegal start of simple expression
val filtered = tupleList.takeWhile( case (s1, s2) => s1 == s2 )
^

scala> val filtered = tupleList.takeWhile{ case (s1, s2) => s1 == s2 }
filtered: List[(String, String)] = List()

if you need declarations, multiple statements, an import or anything like that, you need curly braces. And because an expression is a statement, parenthesis may appear inside curly braces. But the interesting thing is that blocks of code are also expressions, so you can use them anywhere inside an expression:

1 // literal – 字面量
(1) // expression – 表达式
{1} // block of code – 代码块
({1}) // expression with a block of code – 表达式里是一个语句块
{(1)} // block of code with an expression – 语句块里是一个表达式
({(1)}) // you get the drift… – 你懂的。。。。

Deeplearn, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明scala 语法糖之括号论

• 版权声明

本站的文章和资源来自互联网或者站长
的原创，按照 CC BY -NC -SA 3.0 CN
协议发布和共享，转载或引用本站文章
应遵循相同协议。如果有侵犯版权的资
源请尽快联系站长，我们会在24h内删
除有争议的资源。
• 网站驱动

• 友情链接

• 支持主题