[
https://issues.apache.org/jira/browse/GROOVY-10025?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17318539#comment-17318539
]
Dario Arena edited comment on GROOVY-10025 at 4/10/21, 4:53 PM:
----------------------------------------------------------------
[~daniel_sun] GINQ is very nice, i didn't know that this was coming in Groovy
4. There is even an entry in the changelog under "New Features" but i missed
it. :P (maybe when Groovy 4 will come out it would need more "highlighting").
[~emilles] These are all very good ways to implement the "every statement is
indeed an expression". I was thinking however of a more direct support from the
language itself without doing the extra step of implementing these constructs.
Maybe this could be an idea to consider for a future version of Groovy (5.0)
was (Author: darioarena87):
[~abdulsubahan] GINQ is very nice, i didn't know that this was coming in Groovy
4. There is even an entry in the changelog under "New Features" but i missed
it. :P (maybe when Groovy 4 will come out it would need more "highlighting").
[~emilles] These are all very good ways to implement the "every statement is
indeed an expression". I was thinking however of a more direct support from the
language itself without doing the extra step of implementing these constructs.
Maybe this could be an idea to consider for a future version of Groovy (5.0)
> "Assimilate" expressions from other languages (Scala, Python...)
> ----------------------------------------------------------------
>
> Key: GROOVY-10025
> URL: https://issues.apache.org/jira/browse/GROOVY-10025
> Project: Groovy
> Issue Type: Improvement
> Components: Compiler
> Affects Versions: 4.0.0-alpha-2
> Reporter: Dario Arena
> Priority: Minor
> Labels: expression, for, if, semantic, statement,
> switch-statement, syntax
>
> In some other languages (Scala, Python, Haskell, Java 14 switches) every
> statement like if..else, for, switch... is indeed an expression that returns
> a value (usually the last one computed is implicitly returned as in Groovy).
> Would it be possible to include something like conditional expression, for
> comprehensions and switch expressions as a native groovy feature? I think it
> would simplify writing DSLs and maybe in some cases making groovy code neater
> and readable
> Using closures it is possible to write "conditional expressions" without
> using the conditional operator, something like:
> {quote}{{def pillToTake = \{ if(candidate.name == "Neo") "red" else "blue"
> }.call()}}{quote}
> but maybe if groovy supports expression this could be written as
> {quote}{{def pillToTake = if candidate.name == "Neo" then "red" else
> "blue"}}{quote}
> or maybe using switch expression to include more complex cases
> {quote}def trainingSchedule = switch(date) \{
> case \{ isHoliday(it) }: return ['Rest']
> case \{ date.dayOfWeek in [MONDAY, FRIDAY] }: return ['Run 15m', '40
> Push-Ups', '20 Crunches']
> case \{date.dayOfWeek.is(WEDNESDAY) }: return ['Run 10m', '50
> Tractions', '30 Squats']
> default: return ['Run 30m']
> }{quote}
> I've not really dug deep in how the groovy compiler works but maybe
> recognizing these expression, "wrapping" them in a closure and calling that
> closure could be done automatically?
> For comprehensions maybe are a little bit more tricky to implement as a
> native groovy feature and using .collect(..) the code is equally readable,
> but maybe in some cases one could prefer to use a different construct
> Actual groovy code:
> {quote}def prices = stocks.filter \{ it.market == "NASDAQ"}.collect\{
> getPriceFor(it) \}{quote}
> Using for comprehension:
> {quote}def prices = [getPriceFor(stock) for stock in stocks if stock.market
> == "NASDAQ"]{quote}
> It is just a matter of preference which one is more readable but this could
> simplify the "flow" of the code in some cases.
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)