如何跳过Scala中的可选参数? [英] How to skip optional parameters in Scala?
本文介绍了如何跳过Scala中的可选参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定以下带有可选参数的函数:
Given the following function with optional parameters:
def foo(a:Int = 1, b:Int = 2, c:Int = 3) ...
我想保留 a 的默认值,但仅通过位置分配(而不是通过 a)将新值传递给 b 和 c命名赋值),这是以下任何一种语法都很好:
I want to keep the default value of a but pass new values to b and c only by positional assignment (rather than by a named assignment), that is any of the following syntaxes would be nice:
foo( , 5, 7)
foo(_, 5, 7)
Scala 可以实现类似的功能吗?
Is something like that possible with Scala?
推荐答案
没有办法跳过参数,但是你可以在调用你的 foo
方法时使用命名参数,例如:>
There's no way to skip the parameters, but you can use named parameters when you call your foo
method, for example:
// Call foo with b = 5, c = 7 and the default value for a
foo(b = 5, c = 7)
edit - 您专门询问了如何通过位置分配来做到这一点:这在 Scala 中是不可能的.
edit - You asked specifically how to do this by positional assignment: this is not possible in Scala.
这篇关于如何跳过Scala中的可选参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文