【问题标题】:Groovy named parameters cause parameter assignments to switch--any way around this?Groovy 命名参数会导致参数分配切换——有什么办法吗?
【发布时间】:2013-03-01 21:33:34
【问题描述】:

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 参数,它就只是空的?

【问题讨论】:

  • Bill K,用一个非常简单的解决方案更新了我的答案。希望对您有所帮助。

标签: groovy named-parameters


【解决方案1】:

在参数上设置默认值会创建从左到右组合的重载方法,因此很难创建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)

还有一个带有 map 参数的完全类型化方法:

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,并且会发生冲突。

【讨论】:

  • 很好的分析。我倾向于单地图解决方案,但它使使用它有点困难(类型安全,简单一目了然的参数列表......),但这可能是我们在当前 groovy 中能做的最好的事情跨度>
  • 太棒了。我喜欢解决方案 3!非常感谢您的时间和坚持@Will。我敢打赌,有一种方法可以从注释和 methodMissing 生成转发签名(这些类已经扩展了一个基类,所以我确信我可以让它完全按照我想要的方式工作)。我希望有办法为此奖励额外的代表——也许我可以用赏金,我需要调查一下!
  • 顺便说一句,我想做的是创建一个“类”,其中每个方法都可以从命令行运行,也可以从代码中轻松调用(一个类有很多小“脚本”在里面)。我目前使用 (List, Map) 并将 CLI 参数以“a = b”的形式解析到映射中,并将其余的参数按顺序放入列表中——但是有了这个签名,如何调用并不明显它来自代码,我总是要考虑列表的参数/顺序。
  • 不确定我明白了...你有一个解析groovy CliParser.groovy "coolClazz.method1(a, b, c, 90.0)"的类?
  • 目前我不使用 CliParser,但我可能会在我正在进行的下一次迭代中使用它——现在我手动解析它。每个“脚本”都扩展了父类——父类解析命令行,查看第一个参数,在子类中找到一个以相同方式命名的方法,并使用其余参数调用它。每个类上的注释允许我在任何类中打印可用“脚本”(方法,带有注释中的文档文本)的列表。除了参数笨拙之外,它现在运行良好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 2011-11-16
  • 2021-04-10
  • 2023-03-02
  • 2018-03-10
  • 2015-05-13
相关资源
最近更新 更多