On 27.07.20 18:13, MG wrote: [...]
Continously reassigning to methodChosen to itself once it has been set (or in your code: Once it has acquired a value that is Groovy-true) seems confusing & inelegant to me.
I am actually using this style quite often, because of a lack of good alternatives. In the groovy code base you will find several places looking like this: foo = foo || m1() foo = foo || m2() foo = foo || m3() foo = foo || m4() or foo = m1() if (foo == null) foo = m2() if (foo == null) foo = m3() if (foo == null) foo = m4() depending on if foo is a boolean or not [...]
or if adjustArguments is an identity operation if the type argument is null (to me it is more elegant if the doChooseMethod call appears only once): def chooseMethod(String methodName,Object[] arguments) { def methodChosen = null [null, Character.TYPE,Integer.TYPE ].find{ methodChosen = doChooseMethod(methodName, adjustArguments(arguments, it))} if(methodChosen) {return methodChosen } else {throw new GroovyRuntimeException("$methodNamenot found") } }
well, with the streams API:
return Stream.of(null,Character.TYPE,Integer.TYPE). map {doChooseMethod(methodName, adjustArguments(arguments, it)}. findFirst {it}. orElseThrow {new GroovyRuntimeException("$methodName not found")}
The key here being that map is not executed eager.
(Disclaimer: Dry programmed code again, so can contain stupid mistake(s))
dito bye Jochen