Scala可变参数列表,命名参数和参数缺省详解
作者:moonandstar08 时间:2022-09-26 21:00:49
重复参数 Scala在定义函数时允许指定最后一个参数可以重复(变长参数),从而允许函数调用者使用变长参数列表来调用该函数,Scala中使用“*”来指明该参数为重复参数。例如:
scala> def echo (args: String *) =
| for (arg <- args) println(arg)
echo: (args: String*)Unit
scala> echo()
scala> echo ("One")
One
scala> echo ("Hello","World")
Hello
World
在函数内部,变长参数的类型,实际为一数组,比如上例的String * 类型实际为 Array[String]。 然而,如今你试图直接传入一个数组类型的参数给这个参数,编译器会报错:
scala> val arr= Array("What's","up","doc?")
arr: Array[String] = Array(What's, up, doc?)
scala> echo (arr)
<console>:10: error: type mismatch;
found : Array[String]
required: String
echo (arr)
^
为了避免这种情况,你可以通过在变量后面添加 _*来解决,这个符号告诉Scala编译器在传递参数时逐个传入数组的每个元素,而不是数组整体。
scala> echo (arr: _*)
What's
up
doc?
命名参数 通常情况下,调用函数时,参数传入和函数定义时参数列表一一对应。
scala> def speed(distance: Float, time:Float) :Float = distance/time
speed: (distance: Float, time: Float)Float
scala> speed(100,10)
res0: Float = 10.0
使用命名参数允许你使用任意顺序传入参数,比如下面的调用:
scala> speed( time=10,distance=100)
res1: Float = 10.0
scala> speed(distance=100,time=10)
res2: Float = 10.0
缺省参数值 Scala在定义函数时,允许指定参数的缺省值,从而允许在调用函数时不指明该参数,此时该参数使用缺省值。缺省参数通常配合命名参数使用,例如:
scala> def printTime(out:java.io.PrintStream = Console.out, divisor:Int =1 ) =
| out.println("time = " + System.currentTimeMillis()/divisor)
printTime: (out: java.io.PrintStream, divisor: Int)Unit
scala> printTime()
time = 1383220409463
scala> printTime(divisor=1000)
time = 1383220422
来源:https://www.cnblogs.com/moonandstar08/p/5240319.html
标签:Scala,可变参数列表,命名参数,参数缺省
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中ReentrantLock4种常见的坑
2021-09-26 10:51:46
![](https://img.aspxhome.com/file/2023/3/61133_0s.webp)
Redis6搭建集群并在SpringBoot中使用RedisTemplate的实现
2023-10-31 14:48:05
![](https://img.aspxhome.com/file/2023/2/58622_0s.png)
Mybatis的几种传参方式详解
2021-12-08 15:35:53
Spring运行时动态注册bean的方法
2023-11-25 04:16:58
idea输入sout无法自动补全System.out.println()的问题
2023-11-28 21:34:03
![](https://img.aspxhome.com/file/2023/3/60603_0s.jpg)
android使用flutter的ListView实现滚动列表的示例代码
2023-06-26 09:00:13
![](https://img.aspxhome.com/file/2023/8/97858_0s.gif)
Java设计模式之单例和原型
2023-11-29 04:14:18
java实现简单的英文文本单词翻译器功能示例
2023-11-28 10:22:15
![](https://img.aspxhome.com/file/2023/8/59178_0s.png)
通过源码角度看看AccessibilityService
2023-07-25 09:31:43
![](https://img.aspxhome.com/file/2023/2/84752_0s.jpg)
详解Mybatis通用Mapper介绍与使用
2023-11-29 08:49:08
Servlet3.0实现文件上传的方法
2023-08-15 00:52:44
![](https://img.aspxhome.com/file/2023/3/58143_0s.png)
关于SpringCloudStream配置问题
2023-08-08 20:12:00
![](https://img.aspxhome.com/file/2023/0/58050_0s.jpg)
Netty序列化深入理解与使用
2023-05-24 20:13:07
![](https://img.aspxhome.com/file/2023/8/60758_0s.png)
Mybatis-Plus使用updateById()、update()将字段更新为null
2023-11-26 01:53:42
使用Springboot根据配置文件动态注入接口实现类
2022-11-18 06:56:45
简单了解4种分布式session解决方案
2023-08-09 11:45:49
![](https://img.aspxhome.com/file/2023/7/57717_0s.png)
Java使用Collections工具类对List集合进行排序
2022-10-12 23:59:12
Java实现查找当前字符串最大回文串代码分享
2023-07-30 04:05:02
Java对象类型的判断详解
2023-07-26 09:55:07
Java读取TXT文件内容的方法
2023-11-23 22:33:41