groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ocs@ocs" <>
Subject Re: DGM for first or default
Date Thu, 18 Oct 2018 17:17:13 GMT
P.S. Oh, and when I am writing anyway — please, do not abuse the “overloaded” methods
which differ just by their argument lists (myself, I consider them always at best suspicious;
mostly plain wrong). Instead of the suggestion below, if something like that is accepted,
it would be much better to change the method name appropriately (say, e.g., firstWithDefault,
firstWithSupplier). Or perhaps named arguments might be (consistently!) used for these things
(e.g., “first(default:value)” or “first(supplier:value)”).

Consider e.g., an array of closures, of which one would want to get the first one with a default
closure... with the definitions suggested below that's going to be fun!

All the best,

> On 18 Oct 2018, at 5:39 PM, Milles, Eric (TR Technology & Ops) <>
> I see there are the following DGMs for getting first element of a "collection":
>   static <T> T first(T[] self)
>   static <T> T first(List<T> self)
>   static <T> T first(Iterable<T> self)
> Is there a simple sequence for getting the first element or a default value if the "collection"
is empty?  If not, may I propose the addition of the following DGMs:
>   static <T> T first(T[] self, T defaultValue)
>   static <T> T first(List<T> self, T defaultValue)
>   static <T> T first(Iterable<T> self, T defaultValue)
>   static <T> T first(T[] self, Closure<T> defaultSupplier)
>   static <T> T first(List<T> self, Closure<T> defaultSupplier)
>   static <T> T first(Iterable<T> self, Closure<T> defaultSupplier)
> Eric M.

View raw message