Groovy 命名参数会导致参数分配切换——有什么方法可以解决这个问题吗?

问题描述 投票:0回答:1

Groovy 会将所有命名参数收集到一个映射中,并将其作为第一个参数传递到方法中。这看起来很简洁,但是在尝试让它工作之后,它似乎真的无法使用。

所以问题是这样的方法:

def method(paramMap, specificVar1 = 7, specificVar2 = 14)

当你用这样的方式调用这个方法时:

method(12, extraValue: "hello")

你得到了你所期望的:

assert 12 == specificVar1
assert 14 == specificVar2
assert [extraValue:"hello"] == paramMap

还不错,有道理。问题是,如果您假设

Map
参数是可选的,那么您最终可能会得到如下值:

method(12)
assert paramMap == 12
assert specificVar1 == 7 // default values
assert specificVar2 == 14

该标量应该进入 SpecificVar,而不是映射。如果我在方法中专门输入地图:

def method(Map paramMap, specificVar1 = 7, specificVar2 = 14)

然后

method(12, extraValue: "hello")
的工作方式与之前一样,但是
method(12)
会抛出
ClassCastException
。这看起来不太好用。有什么方法可以使
Map
“粘性”,以便在没有
Map
参数时它只是空的吗?

注:

十年后,由于有人投票赞成,我重新审视了这个问题。接受的答案的重载解决方案是完美的。基本上是第二种方法:

def method(specificVar1 = 7, specificVar2 = 14) 
{
    method([:],specificVar1, specificVar2)
}

似乎适用于我的所有测试用例,无论是否有默认值。

如果我要进一步追求这个目标,我可能会尝试找出如何为我喜欢从 repl 运行的一堆方法综合生成这样的重载。

groovy named-parameters
1个回答
14
投票

对参数设置默认值会创建从左到右组合的重载方法,因此,很难制作

method(12)
并且还能够传递映射条目。

您的方法

def method(paramMap, specificVar1=7, specificVar2=14)
将生成以下方法:

Object Maps.method(java.lang.Object)
Object Maps.method(java.lang.Object,java.lang.Object)
Object Maps.method(java.lang.Object,java.lang.Object,java.lang.Object)

以及带有地图参数的完全类型化方法:

def method3(Map paramMap=[:], Integer specificVar1=7, Integer specificVar2=14) {
}

会生成以下方法:

Object Maps.method3()
Object Maps.method3(java.util.Map)
Object Maps.method3(java.util.Map,java.lang.Integer)
Object Maps.method3(java.util.Map,java.lang.Integer,java.lang.Integer)

(没有合适的方法

method(12)
)。

此外,传递给该方法的条目将被收集并插入到第一个 map 参数中。方法如下:

def method4(Integer specificVar1=7, Integer specificVar2=14, Map map=[:]) {

生成:

Object Maps.method4()
Object Maps.method4(java.lang.Integer)
Object Maps.method4(java.lang.Integer,java.lang.Integer)
Object Maps.method4(java.lang.Integer,java.lang.Integer,java.util.Map)

因此,

method4 12, a:'b'
失败并显示:

No signature of method: Maps.method4() is applicable for argument types: 
  (java.util.LinkedHashMap, java.lang.Integer) values: [[a:b], 12]

所以,不,我认为你不能使用地图做你想做的事:-)。


解决方案1:

如果您需要纯动态解决方案,则可以使用单个映射参数:

def method5(Map map) {
  def specificVar1 = map.specificVar1 ?: 7
  def specificVar2 = map.specificVar2 ?: 14
}

解决方案2(已更新):

您可以创建一个类来表示参数。使用映射强制转换为对象是可静态编译的,并且是它的语法糖。

@groovy.transform.CompileStatic
class Maps {
  def method6(Foo foo) { "$foo.params, $foo.specificVar1, $foo.specificVar2" }
  def method6(Map map) { method6 map as Foo }

  static main(args) {
    def maps = new Maps()

    assert maps.method6(params: [a: 'b', c: 'd'], specificVar1: 40) ==
        "[a:b, c:d], 40, 14"

    assert maps.method6(new Foo(params: [a: 'b', c: 'd'], specificVar2: 21)) == 
        "[a:b, c:d], 7, 21"
  }
}

class Foo {
  def specificVar1 = 7, specificVar2 = 14, params = [:]
}

解决方案3:

重载方法。

def method6(Map paramMap, Integer specificVar1=7, Integer specificVar2=14) {
  "$paramMap, $specificVar1, $specificVar2"
}

def method6(Integer specificVar1=7, Integer specificVar2=14) {
  method6 [:], specificVar1, specificVar2
}


assert method6( 12 ) == "[:], 12, 14"
assert method6( ) == "[:], 7, 14"
assert method6( a:'b', 18 ) == "[a:b], 18, 14"
assert method6( 18, a:'b', 27 ) == "[a:b], 18, 27"
assert method6( 90, 100 ) == "[:], 90, 100"
assert method6( a:'b', 140, c:'d' ) == "[a:b, c:d], 140, 14"

地图版本方法不能有默认参数,否则两种方法都会生成无参数

method6
,从而产生冲突。

© www.soinside.com 2019 - 2024. All rights reserved.