scala中的=>符号的含义
【声明】本帖的内容是copy来的,来源为stack overflow。
It 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 forFunction<n>[A[,B,...],T]
, that is, a function that takes parameters of typeA[,B...]
, and returns a value of typeT
.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 typeUnit
, 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
.
=>
is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class.
For example, the type Int => String
, is equivalent to the type Function1[Int,String]
i.e. a function that takes an argument of type Int
and returns a String
. scala> val f: Function1[Int,String] = myInt => "my int: "+myInt.toString
f: (Int) => String = <function1> scala> f(0)
scala> val f2: Function2[Int,Int,String] = (myInt1,myInt2) => "This is my function to transfer " + myInt1 + " and " + myInt2 + " as a string component."
res0: String = my int: 0 scala> val f2: Int => String = myInt => "my int v2: "+myInt.toString
f2: (Int) => String = <function1> scala> f2(1)
res1: String = my int v2: 1
f2: (Int, Int) => String = <function2> scala> f2(1,2)
res6: String = This is my function to transfer 1 and 2 as a string component.
scala> val f22:(Int,Int)=>String = (myInt1,myInt2) => "This is my function to transfer " + myInt1 + " and " + myInt2 + " as a string component."
f22: (Int, Int) => String = <function2>
scala> f22(2,4)
res7: String = This is my function to transfer 2 and 4 as a string component.
Here myInt
is binded to the argument value passed to f
and f2
.
() => T
is the type of a function that takes no arguments and returns a T
. It is equivalent to Function0[T]
. ()
is called a zero parameter list I believe.
scala> val f: () => Unit = () => { println("x")}
f: () => Unit = <function0>
scala> f()
x
scala> val f2: Function0[Unit] = () => println("x2")
f: () => Unit = <function0>
scala> f2()
x2
As the most simplified answer, you can substitute whatever is on the left-hand side of => with the word "LEFT" and whatever is on the right-hand side with the word "RIGHT".
Then, the meaning of "LEFT => RIGHT" becomes:
Take LEFT then do RIGHT.
This means that if you have a "()=>" that you can take nothing (that is, no parameters) and then do whatever is on the right-hand side.
This is the most common meaning.
从而可以看出,这个符号主要是用在函数(匿名)的定义中。慢慢体会。这个是和java等其他语言有较大差别的地方。。。
scala中的=>符号的含义的更多相关文章
- SVN版本控制器中各符号的含义
SVN符号的含义 项目开发过程中,随着学习的不断深入,开始慢慢接触到版本管理控制工具,其实这个工具主要用于团队开发之中,但对于个人项目的备份也有好处,可以避免在电脑出现不可预知的故障时,最大化的保护自 ...
- java泛型中特殊符号的含义
java泛型中的标记符含义: E - Element (在集合中使用,因为集合中存放的是元素) T - Type(Java 类) K - Key(键) V - Value(值) N - Number ...
- makefile中一些符号的含义
关于gnu make的详细介绍参看http://www.gnu.org/software/make/manual/make.html 规则 让我们先来粗略地看一看Makefile的规则. targ ...
- SVN中图标符号的含义
黄色感叹号(有冲突): 这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许你提交,防止你的提交覆盖了别人的 ...
- SVN中各种符号箭头含义
黄色感叹号(有冲突): -- 这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许 你提交,防止你的提交覆盖 ...
- shell 中各种符号的含义
http://yesjavame.iteye.com/blog/1062405 http://blog.csdn.net/taiyang1987912/article/details/39551385
- scala中常用但其他语言不常见的符号含义
本文旨在介绍Scala在其他语言中不太常见的符号含义,帮助理解Scala Code. 随着我对Scala学习的深入,我会不断增加该篇博文的内容. 修改记录 ----2016.11.23 新增scal ...
- scala中常用特殊符号
参考资料: scala中常用但其他语言不常见的符号含义 Scala学习六:Scala中的特殊字符 =>(匿名函数) 参考文档:scala => 用法 匿名函数 => 匿名函数,在Sp ...
- 从jvm来看,scala中的@究竟是个什么鬼?@模式匹配符号(scala 词法分析 语法分析常用)
从jvm来看,scala中的@究竟是个什么鬼? 我也是初步尝试来看jvm的类文件,又是初次来分析@,如不对的地方,请各位指正! 先看一下@ 是个什么? object TestScala { def m ...
随机推荐
- Objective-C--- 多态 、 协议
1 编写交通工具程序 1.1 问题 本案例需要创建一个TRTransportation类,类中有一个方法叫print的方法,该方法默认输出 “显示交通工具信息”,这个类作为父类,派生出三个子类TRTa ...
- 重定位shell
http://www.tldp.org/LDP/abs/html/io-redirection.html http://unix.stackexchange.com/questions/20469/d ...
- android布局中的divider(目前只知道TableLayout)
目前在genymotion中设置了之后显示不出来行与行之间的分割线,但是在真机上面是没有问题的 1.使用xml属性添加(3.0以上版本) 设置LinearLayout标签的 android:showD ...
- strong和b
strong和b标签都是很久以前遗留下来的标签,b标签用来加粗字体,strong用来强调,通常浏览器会把强调的语句加粗,所以二者效果比较近似.语义化愈发受重视以后,b标签退出大众视野,strong依然 ...
- ANTLR3完全参考指南读书笔记[05]
前言 仅生成给出true/false的识别器是没有多大用处的,自然的就有在识别过程中遇到某一结构时执行一段代码.存储该结构中信息的想法. ANTLR提供了在文法中嵌入属性和动作超级混合“文法”,可以生 ...
- meta 标签 关键字 用处
您的个人网站即使做得再精彩,在“浩瀚如海”的网络空间中,也如一叶扁舟不易为人发现,如何推广个人网站, 人们首先想到的方法无外乎以下几种: l 在搜索引擎中登录自己的个人网站 l 在知名网站加入你个人网 ...
- 310. Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- js如何把字符串转换成json数据的方法
js如何把字符串转换成json数据的方法 function strtojson(str){ var json = eval('(' + str + ')'); return json; } 方法二 f ...
- Apache的https协议配置
一.http协议和https协议的传输格式 http:文本格式的协议 https:二进制格式的协议 二.x509.3证书格式: 证书格式的版本号 证书序列号 证书签名算法 证书颁发者 有效期 持有者的 ...
- 1-1 Windows应用程序的特点
主要内容:介绍Windows应用程序的特点,并附加了消息和事件的一些区别 //以后该分类中字体均采用 隶书 4(14pt) 1. 面向对象 <1>针对Windows应用本身,如记事本界面, ...