groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Dawson <david.daw...@simplicityitself.com>
Subject Re: About if statement in the right hand of assignment statement
Date Thu, 28 Sep 2017 10:53:44 GMT
Hi Daniel,

I would suggest that there are existing edge cases that also look very
ugly. I could write

if ( x == "hello" ? [] : false) { }

and so on and get some very odd looking behaviour. Expression based flow
control has some fascinating use cases, and I think it would be of great
value to at the very least explore them, since this is being discussed.

This is actually something being floated for future Java too, this just
appeared in InfoQ -> https://www.infoq.com/news/
2017/09/pattern-matching-for-java

Pattern matching expressions are a core use case for this type of syntax.
They are proposing to do it somewhat differently, introducing a secondary
expression form. My ideal would be to have a single form, but meh, I'm not
aware of how this would end up being implemented and the implications of
that. What they describe in there though is one thing I would love to have
in groovy.

The full concept of pattern matching could be included in this discussion
too, if there is consensus at all, or perhaps . For a 3.0 feature, having a
full dynamic pattern matching expression syntax would be really, really
cool. Leapfrogging java, scala and the rest with something that, I at least
find so very useful and succinct and expressive.

Something like the following would be my ideal, combining pattern matching
and match-as-an-expression.  This is the same idea as a switch, but as an
expression.

def data = ...

def newval = match(data) {
   "simple": 54321
   "hello": 12345
   ~/stuff/: 9999                   // perhaps also destructure the regex
pattern?
   default: 0
}

It could be useful for Optionals or other container types to destructure
them a little too?  This is clunky, no idea how this could really look. The
aim is to pull out bits of the object during a match

Optional<String> opt = ...

def val = match(opt) {
   it.isPresent(): x:it.get():  x[0..2]
   !it.isPresent(): "nothing here
}

Anyway, I've gotten a little over excited I suspect. This kind of thing
would be awesome to have in Groovy.

Summarised

expression oriented control flow
pattern matching
destructuring during control flow + pattern matching

David.


On 27 September 2017 at 14:58, Daniel Sun <realbluesun@hotmail.com> wrote:

> Hi David,
>
>       If we treat if/switch statements as real expression, we may have some
> ugly code like the following, so I just plan to support declaration and
> assignment.
>
> ```
>    if( if (...) { ... } else if (...) { ... } else if (...) {...} else
> {...}
> ) {
>       ...
>    } else if ( if (...) { ... } else if (...) { ... }  ) {
>       ...
>    }
> ```
>
> Cheers,
> Daniel.Sun
>
>
>
> --
> Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
>

Mime
View raw message