首页 > 其他 > 详细

"=>" 在scala 中的意思

时间:2015-10-26 02:07:12      阅读:367      评论:0      收藏:0      [点我收藏+]

?

目前仅知道和匿名函数有关

=>?
1、 匿名函数定义, 左边是参数 ?右边是函数实现体 ?(x: Int)=>{}
2、函数作为参数的定义,左边是参数类型,右边是方法返回值类型 ? ? ? ? ? ? ? ?(Int)=>(Int)

?

不过stackoverflow?上关于这方面的讨论挺多的,故在此记录一下

?

比如来自?http://stackoverflow.com/questions/6951895/what-does-and-mean-in-scala?的一位的回复:

?

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

  • In a value, it introduces a function literal, or?lambda. e.g. the bit inside the curly braces in?List(1,2,3).map { (x: Int) => x * 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 for?Function<n>[A[,B,...],T], that is, a function that takes parameters of type?A[,B...], and returns a value of type?T.

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

    • 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.

  • 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.

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

?

?

?

?

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

?

http://stackoverflow.com/questions/7888944/scala-punctuation-aka-symbols-and-operators

"=>" 在scala 中的意思

原文:http://zhouchaofei2010.iteye.com/blog/2251731

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!