本文介绍了如何跳过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中的可选参数?
发布评论