groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Milles, Eric (TR Technology & Ops)" <>
Subject Re: DGM for first or default
Date Thu, 18 Oct 2018 17:19:42 GMT
"list?.first() ?: defaultValue" is not the equivalent.  If the collection is empty, first()
throws an IndexOutOfBoundsException is thrown.  That's why I'm asking if there is a simple
equivalent.  I suppose this is the equivalent now that I think about it:

list ? list.first() : defaultValue

From: ocs@ocs <>
Sent: Thursday, October 18, 2018 12:07 PM
Subject: Re: DGM for first or default

Myself, I am not a huge fan of adding not-often-needed functionalities (and actually would
add almost none of those discussed lately); nevertheless...

On 18 Oct 2018, at 6:48 PM, Paolo Di Tommaso <<>>

-1, it can be easily done as:
list.first() ?: defaultValue

... this won't work in case the first object is a Groovy False (e.g., an empty string, or
a plethora of others).

All the best,


On Thu, Oct 18, 2018 at 6:45 PM Daniel.Sun <<>>
+0 from me.
P.S. we should add similar DGM for `last` too?


Daniel Sun
Apache Groovy committer
Twitter: @daniel_sun

Sent from:<>

View raw message