Hi Daniil,

1. without "??:", that only works for the case where Groovy-truth is
   applicable for the return value of doChooseMethod
2. I find the [null, Character.TYPE, Integer.TYPE]-iteration idea
   clearer / more DRY here irrespective of that :-)
3. Why _is_ "throw" not considered an expression in Groovy, and should
   we remedy that... G-)
4. Who is Johan ?-)

Cheers,
mg

On 30/07/2020 16:03, Daniil Ovchinnikov wrote:
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