浅谈 Scala 中下划线的用途
Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之外,还有语法层面的,比如 underscore(下划线)就会出现在多种场合,令初学者相当疑惑,今天就来总结下 Scala 中下划线的用法。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
1、存在性类型:Existential typesdef foo(l: List[Option[_]]) = ...2、高阶类型参数:Higher kinded type parameterscase class A[K[_],T](a: K[T])3、临时变量:Ignored variablesval _ = 54、临时参数:Ignored parametersList(1, 2, 3) foreach { _ => println("Hi") }5、通配模式:Wildcard patternsSome(5) match { case Some(_) => println("Yes") }val (a, _) = (1, 2)for (_ <- 1 to 10)6、通配导入:Wildcard importsimport java.util._7、隐藏导入:Hiding importsimport java.util.{ArrayList => _, _}8、连接字母和标点符号:Joining letters to punctuationdef bang_!(x: Int) = 59、占位符语法:Placeholder syntaxList(1, 2, 3) map (_ + 2)_ + _ 10、偏应用函数:Partially applied functionsList(1, 2, 3) foreach println _11、初始化默认值:default valuevar i: Int = _12、访问元组:tuple getterst._2 13、参数序列:parameters Sequence _*作为一个整体,告诉编译器你希望将某个参数当作参数序列处理!例如val s = sum(1 to 5:_*)就是将1 to 5当作参数序列处理。 |
这里需要注意的是,以下两种写法实现的是完全不一样的功能:
|
1
2
3
|
foo _ // Eta expansion of method into method valuefoo(_) // Partial function application |
Example showing why foo(_) and foo _ are different:
|
1
2
3
4
5
6
7
8
|
trait PlaceholderExample { def process[A](f: A => Unit) val set: Set[_ => Unit] set.foreach(process _) // Error set.foreach(process(_)) // No Error} |
In the first case, process _ represents a method; Scala takes the polymorphic method and attempts to make it monomorphic by filling in the type parameter, but realizes that there is no type that can be filled in for A that will give the type (_ => Unit) => ? (Existential _ is not a type).
In the second case, process(_) is a lambda; when writing a lambda with no explicit argument type, Scala infers the type from the argument that foreach expects, and _ => Unit is a type (whereas just plain _ isn't), so it can be substituted and inferred.
This may well be the trickiest gotcha in Scala I have ever encountered.
Refer:
[1] What are all the uses of an underscore in Scala?
http://stackoverflow.com/questions/8000903/what-are-all-the-uses-of-an-underscore-in-scala
[2] Scala punctuation (AKA symbols and operators)
[3] Scala中的下划线到底有多少种应用场景?
http://www.zhihu.com/question/21622725
[4] Strange type mismatch when using member access instead of extractor
[5] Scala简明教程
http://colobu.com/2015/01/14/Scala-Quick-Start-for-Java-Programmers/
浅谈 Scala 中下划线的用途的更多相关文章
- 转载:浅谈 Scala 中下划线的用途
Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之外,还有语法层面的,比如 underscore(下划线)就会出现在多种场合,令初学者相当疑惑,今天就 ...
- Scala 中下划线的用途
转载自:https://my.oschina.net/leejun2005/blog/405305 Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之 ...
- Scala 中下划线的用法
1.存在性类型:Existential types def foo(l: List[Option[_]]) = ... 2.高阶类型参数:Higher kinded type parametersca ...
- 浅谈html5某些新元素的用途
大家都知道html是一种前端网页语言,从出现到现在已经经历了很多的版本了,但是随着html的不断发展,现在的html5已经不再是单一的前端页面语言了,html,javascript,css不再单纯的只 ...
- Scala中下划线的总结
1. 方法转化为函数 2. 集合中的每一个元素 3. 获取元组Tuple中的元素 4. 模式匹配 5. 队列 6. 导包引入的时候 7. 初始化变量 引用自:https://blog.csdn.net ...
- 浅谈Android编码规范及命名规范
前言: 目前工作负责两个医疗APP项目的开发,同时使用LeanCloud进行云端配合开发,完全单挑. 现大框架已经完成,正在进行细节模块上的开发 抽空总结一下Android项目的开发规范:1.编码规范 ...
- Android安全开发之浅谈密钥硬编码
Android安全开发之浅谈密钥硬编码 作者:伊樵.呆狐@阿里聚安全 1 简介 在阿里聚安全的漏洞扫描器中和人工APP安全审计中,经常发现有开发者将密钥硬编码在Java代码.文件中,这样做会引起很大风 ...
- 浅谈对Spring Framework的认识
Spring Framework,作为一个应用框架,官方的介绍如下: The Spring Framework provides a comprehensive programming and con ...
- 浅谈struts2之chain
转自:http://blog.csdn.net/randomnet/article/details/8656759 前一段时间,有关chain的机制着实困绕了许久.尽管网上有许多关于chain的解说, ...
随机推荐
- HDOJ 2152 Fruit(母函数)
Fruit Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Dungeon Game
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- HDU 1316 How Many Fibs?(java,简单题,大数)
题目 /** * compareTo:根据该数值是小于.等于.或大于 val 返回 -1.0 或 1: public int compareTo(BigInteger val) 将此 BigInteg ...
- iOS多线程的初步研究(七)-- dispatch对象
谈起iOS的dispatch(正式称谓是Grand Central Dispatch或GCD),不得不说这又是iOS(包括MacOSX)平台的创新,优缺点这里不讨论,只有当你使用时才能真正体会到.我们 ...
- 基础DOM和CSS操作(一)
DOM简介 DOM是一种文档对象模型,方便开发者对HTML结构元素内容进行展示和修改.在JavaScript中,DOM不但内容庞大繁杂,而且我们开发的过程中需要考虑更多的兼容性.扩展性.在jQuery ...
- Java IO(三)
File File类的常见方法: 1.创建. boolean createNewFile():在指定位置创建文件,如果该文件已经存在,则不创建,返回false.和输出流不一样,输出流对象一建立就创建文 ...
- 使用 Nginx 和 GeoIP 模块来处理不同国家的访问
安装 Nginx因为要用到 http_geoip_module 模块,系统自带的 nginx 一般不带这个模块,所以要下载 nginx 源代码后自行编译: # wget http://nginx.or ...
- jq 幻灯片插件制作
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- Mysql笔记——触发器简单实例
首先贴上触发器语法吧: CREATE TRIGGER <触发器名称> –触发器必须有名字,最多64个字符,可能后面会附有分隔符.它和MySQL中其他对象的命名方式基本相象. { BEFOR ...
- Java:IO流之转换流
IO流的基本流情况: 字符流: 字符流缓冲区: FileReader BufferedReader FileWriter ...