Hi OC,
Let me make the example a bit more complicated to clarify my proposal:
```
def m() {
def a = callA()
if (null != a) return a
def b = callB()
if (b > 10) return b
def c = callC()
if (null != c && c < 10) return c
LOGGER.debug('the default value will be returned')
return defaultValue
}
```
The above code could be simplified as follows:
```
def m() {
return? callA() // same to `return(r -> null != r) callA()`
return(r -> r > 10) callB()
return(r -> null != r && r < 10) callC()
LOGGER.debug('the default value will be returned')
return defaultValue
}
```
Cheers,
Daniel Sun
On 2020/07/25 19:21:01, OCsite <[email protected]> wrote:
> Daniel,
>
> -1. In my personal experience this happens very rarely, not worth a special
> support in the language by far. Almost all similar cases for me are covered
> by simple
>
> def foo() {
> ... ...
> bar()?:defaultValue
> }
>
> and the cases where this is not adequate, either due a non-null false return
> value which should be returned instead of the default, or due to a
> non-trivial code needed at //! below, in my code almost never happen.
>
> Contrariwise, if you are about to improve the language for simplicity and
> easiness of returns, you definitely should consider allowing returning void
> from a void method — this should be a completely valid code (as is e.g., in
> C):
>
> void foo() { ... }
> void bar() {
> return foo()
> }
>
> As always, of course, YMMV.
>
> All the best,
> OC
>
> > On 25 Jul 2020, at 20:55, Daniel Sun <[email protected]> wrote:
> >
> > Hi all,
> >
> > We always have to check the returning value, if it match some
> > condition, return it. How about simplifying it? Let's see an example:
> >
> > ```
> > def m() {
> > def r = callSomeMethod()
> > if (null != r) return r
> > //!
> > return theDefaultResult
> > }
> > ```
> >
> > How about simplifying the above code as follows:
> > ```
> > def m() {
> > return? callSomeMethod()
> //!
> > return theDefaultResult
> > }
> > ```
> >
> > Futhermore, we could make the conditional return more general:
> > ```
> > def m() {
> > return(r -> r != null) callSomeMethod() // we could do more checking,
> > e.g. r > 10
> > return theDefaultResult
> > }
> > ```
> >
> > Any thoughts?
> >
> > Cheers,
> > Daniel Sun
>
>