如何跳过Scala中的可选参数?

编程入门 行业动态 更新时间:2024-10-27 04:31:57
本文介绍了如何跳过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中的可选参数?

本文发布于:2023-07-14 22:15:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可选   跳过   参数   Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!