On Wed, Sep 5, 2018 at 4:58 AM mg <[email protected]> wrote: > Couldn't we rather consider phasing out the whole construct ? Allowing a > ctor to be called without new only in this special case seems awfully > inconsistent... >
I think it has been around for a long time and might be in use in DSLs. I'd probably not encourage it's use in normal programming but see no harm in leacing the existing support. > And with the new @MapConstructor / @NamedVariant/... > and @Newify(pattern=...) support, one can achieve the same thing from > modular annotation building blocks - just without the confusing square > brackets syntax (which does not work for method calls taking a map > argument). > > -------- Ursprüngliche Nachricht -------- > Von: Paul King <[email protected]> > Datum: 04.09.18 00:30 (GMT+01:00) > An: [email protected] > Betreff: Constructor call short-hand syntax > > > Groovy has a rarely used shorthand syntax for constructors: > > println Date[time:0] // same as new Date(time:0) > println Date[year: 118, month: 8, date: 3] // same as new Date(year: 118, > month: 8, date: 3) > > > 1. GROOVY-8602 <https://issues.apache.org/jira/browse/GROOVY-8602>points > out that the safe args version isn't supported, e.g.: > > println Date?[time:0] > > I was thinking of closing this as won't fix since we only support this > shorthand for class constants. > > Any objections? > > Also, I noticed that the empty map isn't catered for: > > println Date[:] // as per above, might be expected to be the same as > new Date([:]) or new Date() > > // currently NPE: Cannot get property '{}' on null object > > So, the map isn't found and the expression becomes "println Date" which > returns void and then we convert the map to a String and look for that > property. > > I realise this is a weird edge case but I was thinking of creating an > issue to fix this for consistency (just Groovy 3). We already support this: > > def map = [:] > println Date[*:map] > > Let me know if you have other thoughts. > > > Cheers, Paul. > >
