About polish the generics type syntax for closure

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

About polish the generics type syntax for closure

Daniel Sun
Hi all

       I've been developing a project with Groovy 3. When I try to specify
the generics type for closure, I have to use annotation..., which is quite
verbose... e.g.
```
public <V> V withSql(@ClosureParams(value= SimpleType.class,
options="groovy.sql.Sql") Closure<V> closure)
```

       I propose make the above code groovier, e.g.
```
public <V> V withSql(Closure<groovy.sql.Sql -> V> closure)
```

       Any thoughts?


Cheers,
Daniel.Sun



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

Re: About polish the generics type syntax for closure

Paolo Di Tommaso
Nice!

On Wed, Feb 13, 2019 at 2:50 AM Daniel Sun <[hidden email]> wrote:
Hi all

       I've been developing a project with Groovy 3. When I try to specify
the generics type for closure, I have to use annotation..., which is quite
verbose... e.g.
```
public <V> V withSql(@ClosureParams(value= SimpleType.class,
options="groovy.sql.Sql") Closure<V> closure)
```

       I propose make the above code groovier, e.g.
```
public <V> V withSql(Closure<groovy.sql.Sql -> V> closure)
```

       Any thoughts?


Cheers,
Daniel.Sun



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

Re: About polish the generics type syntax for closure

Guillaume Laforge
Administrator
That's interesting.
What about the various cases, like no-arg closures?

On Wed, Feb 13, 2019 at 7:37 AM Paolo Di Tommaso <[hidden email]> wrote:
Nice!

On Wed, Feb 13, 2019 at 2:50 AM Daniel Sun <[hidden email]> wrote:
Hi all

       I've been developing a project with Groovy 3. When I try to specify
the generics type for closure, I have to use annotation..., which is quite
verbose... e.g.
```
public <V> V withSql(@ClosureParams(value= SimpleType.class,
options="groovy.sql.Sql") Closure<V> closure)
```

       I propose make the above code groovier, e.g.
```
public <V> V withSql(Closure<groovy.sql.Sql -> V> closure)
```

       Any thoughts?


Cheers,
Daniel.Sun



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


--
Guillaume Laforge
Apache Groovy committer
Developer Advocate @ Google Cloud Platform

Reply | Threaded
Open this post in threaded view
|

Re: About polish the generics type syntax for closure

Daniel Sun
Hi Guillaume,

> What about the various cases, like no-arg closures?
      How about `Closure< -> V>` ?

      It is similar to the syntax of closure. More examples:

1)
`Closure< String, Number -> Date>`

is responding to

```
{ String x, Number y ->
     return new Date()
}
```

2)
`Closure<   -> Date>`

is responding to

```
{  ->
     return new Date()
}
```

3)
`Closure<Date>` means argument generics type information is not available

Cheers,
Daniel.Sun



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

Re: About polish the generics type syntax for closure

Cédric Champeau
I'm 100% sure a syntax like that has been discussed in the past, and discarded for different reasons:

1. consistency between using annotations and a type-checking only feature
2. what about polymorphic closures (aka closures which accept different kind of arguments)
3. the arrow syntax making it hard to read, in particular when the argument types have generics themselves

Happy to reopen the discussion, but please keep this in mind.

Le mer. 13 févr. 2019 à 09:39, Daniel Sun <[hidden email]> a écrit :
Hi Guillaume,

> What about the various cases, like no-arg closures?
      How about `Closure< -> V>` ?

      It is similar to the syntax of closure. More examples:

1)
`Closure< String, Number -> Date>`

is responding to

```
{ String x, Number y ->
     return new Date()
}
```

2)
`Closure<   -> Date>`

is responding to

```
{  ->
     return new Date()
}
```

3)
`Closure<Date>` means argument generics type information is not available

Cheers,
Daniel.Sun



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

Re: About polish the generics type syntax for closure

Daniel Sun
Hi  Cédric,

> I'm 100% sure a syntax like that has been discussed in the past
Well...I missed that discussion...

> 1. consistency between using annotations and a type-checking only feature
I propose to transform `Closure<String, Number -> Date>` to the
annotations(i.e. `@ClosureParams`), so we can keep the consistency.

> 2. what about polymorphic closures (aka closures which accept different
> kind of arguments)
You mean something like `Closure<String, Number -> Date>`?

> 3. the arrow syntax making it hard to read, in particular when the
> argument types have generics themselves
It depends developers' preferences... e.g.

①```
Closure<String, Tuple2&lt;Number, String>  ->  Date>
```
VS
②```
@ClosureParams(value= SimpleType.class, options="String, Tuple2<Number,
String>") Closure<Date>
```

Syntax① looks better to me ;-)


Cheers,
Daniel.Sun





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

Re: About polish the generics type syntax for closure

Cédric Champeau

> 2. what about polymorphic closures (aka closures which accept different
> kind of arguments)
You mean something like `Closure<String, Number -> Date>`?


No, I mean things like each on a map. You can pass it a closure which accepts a Map.Entry (1 parameter), or you can pass it a closure which accepts key, value (2 parameters).

Reply | Threaded
Open this post in threaded view
|

Re: About polish the generics type syntax for closure

Milles, Eric (TR Tech, Content & Ops)
In reply to this post by Daniel Sun

Why not use "public <V> V withSql(java.util.functional.Function<groovy.sql.Sql, V> fn)" in this case?  @ClosureParams and @DelegatesTo are usable in Java code as well.  And they cover a whole range of cases besides 1:1 mapping.  There are a number of SAM types that can be used instead of Closure and you can also define your own.




From: Daniel Sun <[hidden email]>
Sent: Tuesday, February 12, 2019 7:46 PM
To: [hidden email]
Subject: About polish the generics type syntax for closure
 
Hi all

       I've been developing a project with Groovy 3. When I try to specify
the generics type for closure, I have to use annotation..., which is quite
verbose... e.g.
```
public <V> V withSql(@ClosureParams(value= SimpleType.class,
options="groovy.sql.Sql") Closure<V> closure)
```

       I propose make the above code groovier, e.g.
```
public <V> V withSql(Closure<groovy.sql.Sql -> V> closure)
```

       Any thoughts?


Cheers,
Daniel.Sun



--
Sent from: https://urldefense.proofpoint.com/v2/url?u=http-3A__groovy.329449.n5.nabble.com_Groovy-2DDev-2Df372993.html&d=DwICAg&c=4ZIZThykDLcoWk-GVjSLmy8-1Cr1I4FWIvbLFebwKgY&r=tPJuIuL_GkTEazjQW7vvl7mNWVGXn3yJD5LGBHYYHww&m=DFJTyfeM1ifwZi7jWOJzHk2DUeAJqD75ON_ZAz6ArbQ&s=lS5nCK2jlmIDPsVhuCRmcbD4HEHI-hx5ZaBqznfDl34&e=
Reply | Threaded
Open this post in threaded view
|

Re: About polish the generics type syntax for closure

Daniel.Sun
Thanks for your suggestion.

If the argument type is Function and the parameter type is Closure, implicit
coerce will happen, thus the performance will be impacted somehow. Certainly
we can use native lambda to avoid the performance issue.
In addition, Function accepts only one parameter, a bit limited for general
scenarios.

In short, Closure can not be replaced completely.

Cheers,
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
Apache Groovy committer & PMC member

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

Re: About polish the generics type syntax for closure

Milles, Eric (TR Tech, Content & Ops)

I agree Closure cannot be replaced/emulated completely.  And Function is not universally applicable either.  But the universe of SAM types is much bigger than Function alone.


Your proposed syntax does not appear to cover much more than Function would.  And it would introduce a lot of effort for reader and tool builder as well.  Groovy/Java developer already has to be aware of closure literal and lambda literal syntax.  Your proposal adds a third possibility when reader encounters "->" in source.


My general point is that there are a number of options that are available today.  Most can also be used by Java or Kotlin code that offers some Groovy support/interop.  Groovy-only syntax is not as widely applicable and would not be available for some time.  My opinion is that there is little benefit to the new syntax and a lot of potential cost.




From: Daniel.Sun <[hidden email]>
Sent: Wednesday, February 13, 2019 8:07 AM
To: [hidden email]
Subject: Re: About polish the generics type syntax for closure
 
Thanks for your suggestion.

If the argument type is Function and the parameter type is Closure, implicit
coerce will happen, thus the performance will be impacted somehow. Certainly
we can use native lambda to avoid the performance issue.
In addition, Function accepts only one parameter, a bit limited for general
scenarios.

In short, Closure can not be replaced completely.

Cheers,
Daniel.Sun




-----
Apache Groovy committer
Blog: https://urldefense.proofpoint.com/v2/url?u=http-3A__blog.sunlan.me&d=DwICAg&c=4ZIZThykDLcoWk-GVjSLmy8-1Cr1I4FWIvbLFebwKgY&r=tPJuIuL_GkTEazjQW7vvl7mNWVGXn3yJD5LGBHYYHww&m=OEzRyWh0ecDlHuHykn040knCc4zWHNNZDEWY-2anHm8&s=UpwOkqNP45mAdUSjnCO-aR_PyQrVLRKFSrEyUIQSqRY&e= 
Twitter: @daniel_sun

--
Sent from: https://urldefense.proofpoint.com/v2/url?u=http-3A__groovy.329449.n5.nabble.com_Groovy-2DDev-2Df372993.html&d=DwICAg&c=4ZIZThykDLcoWk-GVjSLmy8-1Cr1I4FWIvbLFebwKgY&r=tPJuIuL_GkTEazjQW7vvl7mNWVGXn3yJD5LGBHYYHww&m=OEzRyWh0ecDlHuHykn040knCc4zWHNNZDEWY-2anHm8&s=YwSwy0HdBm2CKcJaoqan8AoB-csth7-8vS6qutDbwt0&e=
12