I think we should consider deprecating and/or hiding such syntax constructs 
behind a compiler switch / @Legacy annotation, otherwise we can never rid 
Groovy of sins from the past.
Short term in my opinion we should definitely "will not fix" GROOVY-8602, and 
leave the [:] edge case as is (ie unsupported), as to not encourage use of the 
syntax.

-------- Ursprüngliche Nachricht --------Von: Paul King <[email protected]> 
Datum: 05.09.18  00:08  (GMT+01:00) An: [email protected] Betreff: Re: 
Constructor call short-hand syntax 


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)
GROOVY-8602points 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.


Reply via email to