groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul King <>
Subject Constructor call short-hand syntax
Date Mon, 03 Sep 2018 22:30:50 GMT
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 <>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

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.

View raw message