【发布时间】:2019-06-03 09:26:43
【问题描述】:
使用documented mixture of named and positional arguments(或带有默认值的参数),如何在不指定任何命名参数的情况下调用方法?
我正在尝试在不破坏其他所有人代码的情况下扩展现有的共享方法,这种方法看起来很有希望,但以下示例失败了:
def test(Map args, some, thing='default value'){
"$some $thing";
}
//good - adding any named parameter works
//test('yet', 'another good', anything:'notneeded');
//but not specifying named parameter fails
test('this', 'fails');
我找不到有关此行为的文档,而且看起来很奇怪。
【问题讨论】:
-
第一个参数是“地图”。我猜调用将是 test(args:null, "this", "pass") 工作正常
-
@Jayan,没错,但这会破坏其他人的代码
-
对我来说,像 test("key":"value", "this", "passed" 这样的方法调用工作正常。您可能希望使用旧方法定义及其调用者和新的预期调用者编辑问题.
-
为什么不把参数的东西放到args map中呢?