Constructor call short-hand syntax

classic Classic list List threaded Threaded
17 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Constructor call short-hand syntax

paulk_asert

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
  2. 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.

Reply | Threaded
Open this post in threaded view
|

Re: Constructor call short-hand syntax

Daniel Sun
Hi Paul,

     Safe index is only for accessing the elements of collections and object
properties, but not for creating instance. The collections and objects may
be null, so we designed the "safe" index.

     To sum up, I vote -1 to support syntax like `Date[time:0]`, the class
instance will not be null.

Cheers,
Daniel.Sun



--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
Reply | Threaded
Open this post in threaded view
|

Re: Constructor call short-hand syntax

paulk_asert

Daniel,

Just to clarify, your vote for -1 is for `Date?[time:0]`. Right?


On Tue, Sep 4, 2018 at 2:49 PM Daniel Sun <[hidden email]> wrote:
Hi Paul,

     Safe index is only for accessing the elements of collections and object
properties, but not for creating instance. The collections and objects may
be null, so we designed the "safe" index.

     To sum up, I vote -1 to support syntax like `Date[time:0]`, the class
instance will not be null.

Cheers,
Daniel.Sun



--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
Reply | Threaded
Open this post in threaded view
|

Re: Constructor call short-hand syntax

Jochen Theodorou
In reply to this post by Daniel Sun


Am 04.09.2018 um 06:49 schrieb Daniel Sun:
> Hi Paul,
>
>       Safe index is only for accessing the elements of collections and object
> properties, but not for creating instance. The collections and objects may
> be null, so we designed the "safe" index.
>
>       To sum up, I vote -1 to support syntax like `Date[time:0]`, the class
> instance will not be null.

I think you actually miss the point a little. Let me rephrase... should
we support

somMap?[x:y]

for example. I personally think we can take supporting things like
x[y:z] and x?[y:z] as future project... if we find a nice use case for this.

bye Jochen
Reply | Threaded
Open this post in threaded view
|

Re: Constructor call short-hand syntax

Daniel.Sun
In reply to this post by paulk_asert
Yep. The safe index of `Date?[time:0]` looks pointless...

Cheers,
Daniel.Sun




-----
Daniel Sun
Apache Groovy committer
Blog: http://blog.sunlan.me 
Twitter: @daniel_sun

--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
Daniel Sun
Apache Groovy committer

Blog: http://blog.sunlan.me
Twitter: @daniel_sun
Reply | Threaded
Open this post in threaded view
|

Re: Constructor call short-hand syntax

Daniel.Sun
In reply to this post by Jochen Theodorou
OK. I wonder what semantic `somMap?[x:y]` should be @_@

Cheers,
Daniel.Sun




-----
Daniel Sun
Apache Groovy committer
Blog: http://blog.sunlan.me 
Twitter: @daniel_sun

--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
Daniel Sun
Apache Groovy committer

Blog: http://blog.sunlan.me
Twitter: @daniel_sun
Reply | Threaded
Open this post in threaded view
|

Re: Constructor call short-hand syntax

daniil.ovchinnikov
Same as someObject?.getAt(x:y)


Daniil Ovchinnikov
JetBrains


On 4 Sep 2018, at 20:23, Daniel.Sun <[hidden email]> wrote:

OK. I wonder what semantic `somMap?[x:y]` should be @_@

Cheers,
Daniel.Sun




-----
Daniel Sun
Apache Groovy committer
Blog: http://blog.sunlan.me
Twitter: @daniel_sun

--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

MG
Reply | Threaded
Open this post in threaded view
|

Re: Constructor call short-hand syntax

MG
In reply to this post by paulk_asert
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....

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 <[hidden email]>
Datum: 04.09.18 00:30 (GMT+01:00)
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
  2. 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.

Reply | Threaded
Open this post in threaded view
|

Re: Constructor call short-hand syntax

paulk_asert
In reply to this post by daniil.ovchinnikov


On Wed, Sep 5, 2018 at 3:24 AM Daniil Ovchinnikov <[hidden email]> wrote:
Same as someObject?.getAt(x:y)

That would be the right mapping but we don't have any getAt(Map) support at the moment - well apart from the ClassConstant constructor short-hand being discussed..
 


On 4 Sep 2018, at 20:23, Daniel.Sun <[hidden email]> wrote:

OK. I wonder what semantic `somMap?[x:y]` should be @_@

Cheers,
Daniel.Sun




-----
Daniel Sun
Apache Groovy committer
Blog: http://blog.sunlan.me
Twitter: @daniel_sun

--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Reply | Threaded
Open this post in threaded view
|

Re: Constructor call short-hand syntax

paulk_asert
In reply to this post by paulk_asert


On Wed, Sep 5, 2018 at 4:58 AM mg <[hidden email]> 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 <[hidden email]>
Datum: 04.09.18 00:30 (GMT+01:00)
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
  2. 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.

12