I agree with Johan here, I’d even go ahead and write something like:
```
def chooseMethod(String methodName, Object[] arguments) {
   final methodChosen = doChooseMethod(methodName, arguments)
       ?: doChooseMethod(methodName, adjustArguments(arguments.clone(), 
Character.TYPE))
       ?: doChooseMethod(methodName, adjustArguments(arguments.clone(), 
Integer.TYPE))
   if (null != methodChosen) return methodChosen
   throw new GroovyRuntimeException("$methodName not found")
}
```
If the language would consider ‘throw' an expression, then:
```
def chooseMethod(String methodName, Object[] arguments) {
   return doChooseMethod(methodName, arguments)
       ?: doChooseMethod(methodName, adjustArguments(arguments.clone(), 
Character.TYPE))
       ?: doChooseMethod(methodName, adjustArguments(arguments.clone(), 
Integer.TYPE))
       ?: throw new GroovyRuntimeException("$methodName not found")
}
```

-1 from me overall.
—

Daniil Ovchinnikov
JetBrains

> On 27 Jul 2020, at 07:00, Jochen Theodorou <blackd...@gmx.org> wrote:
> 
> On 26.07.20 20:23, Daniel Sun wrote:
>> Hi mg,
>> 
>>> maybe you can give some real life code where you encounter this on a 
>>> regular basis ?
>> 
>> Let's think about the case about choosing method by method name and 
>> arguments:
>> 
>> ```
>> def chooseMethod(String methodName, Object[] arguments) {
>>    def methodChosen = doChooseMethod(methodName, arguments)
>>    if (null != methodChosen) return methodChosen
>> 
>>    methodChosen = doChooseMethod(methodName, 
>> adjustArguments(arguments.clone(), Character.TYPE))
>>    if (null != methodChosen) return methodChosen
>> 
>>    methodChosen = doChooseMethod(methodName, 
>> adjustArguments(arguments.clone(), Integer.TYPE))
>>    if (null != methodChosen) return methodChosen
>> 
>>    throw new GroovyRuntimeException("$methodName not found")
>> }
>> ```
> 
> now that I would now maybe write like this:
> 
>>> def chooseMethod(String methodName, Object[] arguments) {
>>>    def methodChosen = doChooseMethod(methodName, arguments)
>>>    methodChosen ?: doChooseMethod(methodName, 
>>> adjustArguments(arguments.clone(), Character.TYPE))
>>>    methodChosen ?: doChooseMethod(methodName, 
>>> adjustArguments(arguments.clone(), Integer.TYPE))
>>>    if (null != methodChosen) return methodChosen
>>> 
>>>    throw new GroovyRuntimeException("$methodName not found")
>>> }
> 
> 
> compared to
> 
>> 
>> The above code could be simplified as:
>> ```
>> def chooseMethod(String methodName, Object[] arguments) {
>>    return? doChooseMethod(methodName, arguments)
>>    return? doChooseMethod(methodName, adjustArguments(arguments.clone(), 
>> Character.TYPE))
>>    return? doChooseMethod(methodName, adjustArguments(arguments.clone(), 
>> Integer.TYPE))
>> 
>>    throw new GroovyRuntimeException("$methodName not found")
>> }
>> ```
> 
> bye Jochen

Reply via email to