浅谈 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 types
def foo(l : List[Option[ _ ]]) = ...
2 、高阶类型参数:Higher kinded type parameters
case class A[K[ _ ],T](a : K[T])
3 、临时变量:Ignored variables
val _ = 5
4 、临时参数:Ignored parameters
List( 1 , 2 , 3 ) foreach { _ = > println( "Hi" ) }
5 、通配模式:Wildcard patterns
Some( 5 ) match { case Some( _ ) = > println( "Yes" ) }
val (a, _ ) = ( 1 , 2 )
for ( _ <- 1 to 10 )
6 、通配导入:Wildcard imports
import java.util. _
7 、隐藏导入:Hiding imports
import java.util.{ArrayList = > _ , _ }
8 、连接字母和标点符号:Joining letters to punctuation
def bang _ !(x : Int) = 5
9 、占位符语法:Placeholder syntax
List( 1 , 2 , 3 ) map ( _ + 2 )
_ + _
10 、偏应用函数:Partially applied functions
List( 1 , 2 , 3 ) foreach println _
11 、初始化默认值:default value
var i : Int = _
12 、访问元组:tuple getters
t. _ 2
13 、参数序列:parameters Sequence
_ *作为一个整体,告诉编译器你希望将某个参数当作参数序列处理!例如 val s = sum( 1 to 5 :_ *)就是将 1 to 5 当作参数序列处理。
|
这里需要注意的是,以下两种写法实现的是完全不一样的功能:
1
2
3
|
foo _ // Eta expansion of method into method value
foo( _ ) // 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)
http://stackoverflow.com/questions/7888944/scala-punctuation-aka-symbols-and-operators/7890032#7890032
[3] Scala中的下划线到底有多少种应用场景?
http://www.zhihu.com/question/21622725
[4] Strange type mismatch when using member access instead of extractor
http://stackoverflow.com/questions/9610736/strange-type-mismatch-when-using-member-access-instead-of-extractor/9610961
[5] Scala简明教程
http://colobu.com/2015/01/14/Scala-Quick-Start-for-Java-Programmers/
转载于:https://www.cnblogs.com/tonychai/p/4546264.html
浅谈 Scala 中下划线的用途相关推荐
- Scala中下划线“_”的用法小结
下划线符号"_"在Scala语言中经常会用到,并且出现的场景千变万化.本文就列举出在一些主流情境下,"_"这个魔法符号分别是什么用法. 匿名函数参数占位符 当匿 ...
- 浅谈Scala 2.8的包对象(package object)
2019独角兽企业重金招聘Python工程师标准>>> Scala 2.8提供包对象(package object)的新特性.什么是包对象呢?按我的理解,根据Scala"一 ...
- scala中下划线的几种含义
目录 1. 将方法转变为函数 2. 表示集合里的每个元素 3. 获取元组Tuple中的元素 4. 模式匹配 5. 队列匹配 6. 通配符 7. 变长参数 8. 初始化变量 1. 将方法转变为函数 de ...
- Scala中下划线的7种用法
Scala是一门以java虚拟机(JVM)为运行环境并将面向对象和函数式编程的最佳特性结合在一起的静态类型编程语言.scala 单作为一门语言来看, 非常的简洁高效,在Scala中存在很多让代码更加简 ...
- Scala 中下划线的用法
1.存在性类型:Existential types def foo(l: List[Option[_]]) = ...2.高阶类型参数:Higher kinded type parameterscas ...
- Scala中下划线“_“的应用场景
1. import导入包的所有成员,相当于java的*,而*在scala中可以作为合法的identifier.比java方便的一点是它可以导入某个类下的所有静态成员,java则需要import sta ...
- 浅谈Scrapy框架之Items用途
初次使用item时,不禁发出疑问,为什么scrapy框架要设计item这个东西,python中不是有dict可以做到数据的传递和存储么?经过查询后,具体总结如下: 爬虫爬取本质是什么? 主要目标是从非 ...
- css中字体下划线样式,css下划线 浅谈css自定义下划线
使用css样式对一段文字或一段文字中其中几个文字设置虚线效果的下划线如何实现?我们知道css字体下划线使用text-decoration样式实现,而虚线下划线则不能使用此css样式属性.要实现通过下边 ...
- 浅谈python 里面的单下划线与双下划线的区别
更新时间:2017年12月01日 10:30:13 作者:空气中的臭氧 这篇文章主要介绍了浅谈python 里面的单下划线与双下划线的区别,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起 ...
最新文章
- 再见!Kafka决定弃用Zookeeper...
- LeetCode Linked List Cycle II
- 互联网1分钟 |1212
- 数学习题:求解不定方程a^2 + b^2 = 3025
- Unreal4 IOS上使用第三方库和C++11 特性问题解决
- 微信端php 开发技术要求,PHP微信开发技术 - WebFalse文档托管平台
- linux 6.5 dos2unix,dos2unix 安装
- K近邻算法(K-NN)
- safari 插件安装之alipay
- java utc时间_Java获得UTC时间
- MicroByte蓝牙手柄初探
- 纳米数据世界杯足球数据api接口,体育数据api接口,实时比分接口示例演示,数据推送
- 人大金仓数据库安装与配置
- 专升本英语——应试题型突破——完形填空——十大解题技巧【学习笔记】
- 苏炳添开始招研究生了!
- 剑指Offer——京东校招笔试题+知识点总结
- 【Unity入门】10.物体的运动
- linux下sybase配置文件,linux下SYBASE数据库安装后的配置
- 进程的三种状态及相互之间的转换
- 如何使用ABBYY FineReader 14创建PDF文档
热门文章
- 区间覆盖全部类型及部分精选习题汇总详解(贪心策略)
- yiilite.php,缓存 - yii在哪些情况下可以加载yiilite.php?
- java对象模型 指令_深入理解多线程(二)—— Java的对象模型
- 绝地求生信号枪只能在服务器吗,绝地求生信号枪怎么用?信号枪刷新点及用法详解...
- java中实现线程互斥的关键词_简单的互斥同步方式——synchronized关键字详解
- linux php oauth安装,Linux php 扩展安装 mongo ,redis ,soap,imap,pdo_mysql,oauth
- 多帧点云数据拼接合并_自动驾驶:Lidar 3D传感器点云数据和2D图像数据的融合标注...
- 华为鸿蒙等不急了,华为最强巨作,鸿蒙OS+六摄+麒麟985,网友:等不及了!
- oracle11g中的join,sql - 使用Oracle 11g中的+符号进行左外连接
- C++(STL):31 ---关联式容器map源码剖析