Scala中“=>”用法及含义

=> has several meanings in Scala, all related to its mathematical meaning as implication.


1. In a value, it introduces a function literal, or lambda. e.g. the bit inside the curly braces inList(1,2,3).map { (x: Int) => x * 2 }


2. In a type, with symbols on both sides of the arrow (e.g. A => T, (A,B) => T, (A,B,C) => T, etc.) it's sugar forFunction<n>[A[,B,...],T], that is, a function that takes parameters of typeA[,B...], and returns a value of type T.


  2.1  Empty parens on the left hand side (e.g. () => T) indicate that the function takes no parameters (also sometimes called a "thunk");


  2.2 Empty parens on the right hand side denote that it returns ()—the sole value of type Unit, whose name can also be written ()—confused yet? :)


A function that returns Unit is also known as a procedure, normally a method that's called only for its side effect.


3. In the type declaration for a method or function parameter, with no symbol on the left hand side (e.g.def f(param: => T)) it's a "by-name parameter", meaning that is evaluated every time it's used within the body of the function, and not before. Ordinary "by-value" parameters are evaluated before entry into the function/method.


4.  In a case clause, they separate the pattern (and optional guard) from the result expression, e.g.case x => y.

 

参考链接:

1. https://stackoverflow.com/questions/6951895/what-does-and-mean-in-scala

2. http://www.aboutyun.com/thread-11934-1-1.html

转载于:https://www.cnblogs.com/hubavyn/p/7927780.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值