why doesn't inject return a generic type?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
40 messages Options
1234
Reply | Threaded
Open this post in threaded view
|

why doesn't inject return a generic type?

HamletDRC2
The inject method is declared as so:

    public static Object inject(Collection self, Object value, Closure closure) {
        return inject(self.iterator(), value, closure);
    }

Why does this not use a generic type?
This would be a better API, wouldn't it?

    public static <T> T inject(Collection self, T value, Closure closure) {
        return inject(self.iterator(), value, closure);
    }

Is there a problem with that?

--
Hamlet D'Arcy
[hidden email]


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: why doesn't inject return a generic type?

Peter Niederwieser
HamletDRC2 wrote
This would be a better API, wouldn't it?

    public static <T> T inject(Collection self, T value, Closure closure) {
        return inject(self.iterator(), value, closure);
    }

Is there a problem with that?
You need to consider both the type of 'value' (which gets returned as-is if the collection is empty), and the return type of the closure. The following should work. I don't remember if I found a problem with it, or if Closure wasn’t generic by then. Probably the latter.

public static <T, U extends T, V extends T> T inject(Collection self, U value, Closure<V> closure)

Cheers,
Peter

Reply | Threaded
Open this post in threaded view
|

Re: why doesn't inject return a generic type?

HamletDRC2
Having the correct generic signatures is hard to write, but it would make tooling easier.

--
Hamlet D'Arcy
[hidden email]

----- Original Message -----

> HamletDRC2 wrote:
> >
> > This would be a better API, wouldn't it?
> >
> >     public static <T> T inject(Collection self, T value, Closure
> >     closure)
> > {
> >         return inject(self.iterator(), value, closure);
> >     }
> >
> > Is there a problem with that?
> >
>
> You need to consider both the type of 'value' (which gets returned
> as-is if
> the collection is empty), and the return type of the closure. The
> following
> should work. I don't remember if I found a problem with it, or if
> Closure
> wasn’t generic by then. Probably the latter.
>
> public static <T, U extends T, V extends T> T inject(Collection self,
> U
> value, Closure<V> closure)
>
> Cheers,
> Peter
>
>
> --
> View this message in context:
> http://groovy.329449.n5.nabble.com/why-doesn-t-inject-return-a-generic-type-tp3377127p3377152.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: why doesn't inject return a generic type?

Alex Tkachman
In reply to this post by Peter Niederwieser
Well, it is not so useful without Closure itself being properly
generified (I mean parameter types)

In g++ we have

    static <T, R> R foldLeft(Iterator<T> self, R init, Function2<T, R, R> op) {
        self.hasNext() ? foldLeft(self, op(self.next(), init), op) : init
    }

I can not say that this generification is ideal but in practice it
sufficient for type inference

On Wed, Feb 9, 2011 at 10:17 AM, Peter Niederwieser <[hidden email]> wrote:

>
>
> HamletDRC2 wrote:
>>
>> This would be a better API, wouldn't it?
>>
>>     public static <T> T inject(Collection self, T value, Closure closure)
>> {
>>         return inject(self.iterator(), value, closure);
>>     }
>>
>> Is there a problem with that?
>>
>
> You need to consider both the type of 'value' (which gets returned as-is if
> the collection is empty), and the return type of the closure. The following
> should work. I don't remember if I found a problem with it, or if Closure
> wasn’t generic by then. Probably the latter.
>
> public static <T, U extends T, V extends T> T inject(Collection self, U
> value, Closure<V> closure)
>
> Cheers,
> Peter
>
>
> --
> View this message in context: http://groovy.329449.n5.nabble.com/why-doesn-t-inject-return-a-generic-type-tp3377127p3377152.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: why doesn't inject return a generic type?

paulk_asert
Administrator
In reply to this post by HamletDRC2

Created an issue:
http://jira.codehaus.org/browse/GROOVY-4671


I have the change locally.
I can commit unless someone else already has it too.


Paul.

On 9/02/2011 5:21 PM, Hamlet DArcy wrote:

> Having the correct generic signatures is hard to write, but it would make tooling easier.
>
> --
> Hamlet D'Arcy
> [hidden email]
>
> ----- Original Message -----
>> HamletDRC2 wrote:
>>>
>>> This would be a better API, wouldn't it?
>>>
>>>      public static<T>  T inject(Collection self, T value, Closure
>>>      closure)
>>> {
>>>          return inject(self.iterator(), value, closure);
>>>      }
>>>
>>> Is there a problem with that?
>>>
>>
>> You need to consider both the type of 'value' (which gets returned
>> as-is if
>> the collection is empty), and the return type of the closure. The
>> following
>> should work. I don't remember if I found a problem with it, or if
>> Closure
>> wasn’t generic by then. Probably the latter.
>>
>> public static<T, U extends T, V extends T>  T inject(Collection self,
>> U
>> value, Closure<V>  closure)
>>
>> Cheers,
>> Peter
>>
>>
>> --
>> View this message in context:
>> http://groovy.329449.n5.nabble.com/why-doesn-t-inject-return-a-generic-type-tp3377127p3377152.html
>> Sent from the groovy - user mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>> http://xircles.codehaus.org/manage_email
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>      http://xircles.codehaus.org/manage_email
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: why doesn't inject return a generic type?

melix
In reply to this post by HamletDRC2
In Groovy (and generally in a dynamically typed language), as far as I know, there's no reason why the return value should be the same type of the injected value. Here's a simple example :

(1..10).inject(1) { x,y -> x/y }

1 is an int, but the result of the injection will be a BigDecimal.

Cédric

Le 09/02/2011 07:58, Hamlet DArcy a écrit :
The inject method is declared as so: 

    public static Object inject(Collection self, Object value, Closure closure) {
        return inject(self.iterator(), value, closure);
    }

Why does this not use a generic type? 
This would be a better API, wouldn't it? 

    public static <T> T inject(Collection self, T value, Closure closure) {
        return inject(self.iterator(), value, closure);
    }

Is there a problem with that? 

--
Hamlet D'Arcy
[hidden email]


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





--

Cédric CHAMPEAU
Responsable du développement logiciel

>LINGWAY
  Des moteurs qui comprennent votre métier
  Immeuble Paritalie
  18-20 rue Pasteur
  94278 Le Kremlin-Bicêtre Cedex
  +33 (0)1 58 46 12 40

Reply | Threaded
Open this post in threaded view
|

Re: why doesn't inject return a generic type?

Alex Tkachman
In reply to this post by paulk_asert
Please put in 1.7.x as well if nobody has objections

On Wed, Feb 9, 2011 at 11:43 AM, Paul King <[hidden email]> wrote:

>
> Created an issue:
> http://jira.codehaus.org/browse/GROOVY-4671
>
>
> I have the change locally.
> I can commit unless someone else already has it too.
>
>
> Paul.
>
> On 9/02/2011 5:21 PM, Hamlet DArcy wrote:
>>
>> Having the correct generic signatures is hard to write, but it would make
>> tooling easier.
>>
>> --
>> Hamlet D'Arcy
>> [hidden email]
>>
>> ----- Original Message -----
>>>
>>> HamletDRC2 wrote:
>>>>
>>>> This would be a better API, wouldn't it?
>>>>
>>>>     public static<T>  T inject(Collection self, T value, Closure
>>>>     closure)
>>>> {
>>>>         return inject(self.iterator(), value, closure);
>>>>     }
>>>>
>>>> Is there a problem with that?
>>>>
>>>
>>> You need to consider both the type of 'value' (which gets returned
>>> as-is if
>>> the collection is empty), and the return type of the closure. The
>>> following
>>> should work. I don't remember if I found a problem with it, or if
>>> Closure
>>> wasn’t generic by then. Probably the latter.
>>>
>>> public static<T, U extends T, V extends T>  T inject(Collection self,
>>> U
>>> value, Closure<V>  closure)
>>>
>>> Cheers,
>>> Peter
>>>
>>>
>>> --
>>> View this message in context:
>>>
>>> http://groovy.329449.n5.nabble.com/why-doesn-t-inject-return-a-generic-type-tp3377127p3377152.html
>>> Sent from the groovy - user mailing list archive at Nabble.com.
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>> http://xircles.codehaus.org/manage_email
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>   http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: why doesn't inject return a generic type?

HamletDRC2
In reply to this post by paulk_asert
I think the following methods would also benefit from generics:
sort()
unique()
multiply()
min()
max()
findResult()
findAll()
find()
asSynchronized()
asImmutable()

--
Hamlet D'Arcy
[hidden email]

----- Original Message -----

> Created an issue:
> http://jira.codehaus.org/browse/GROOVY-4671
>
>
> I have the change locally.
> I can commit unless someone else already has it too.
>
>
> Paul.
>
> On 9/02/2011 5:21 PM, Hamlet DArcy wrote:
> > Having the correct generic signatures is hard to write, but it would
> > make tooling easier.
> >
> > --
> > Hamlet D'Arcy
> > [hidden email]
> >
> > ----- Original Message -----
> >> HamletDRC2 wrote:
> >>>
> >>> This would be a better API, wouldn't it?
> >>>
> >>>      public static<T> T inject(Collection self, T value, Closure
> >>>      closure)
> >>> {
> >>>          return inject(self.iterator(), value, closure);
> >>>      }
> >>>
> >>> Is there a problem with that?
> >>>
> >>
> >> You need to consider both the type of 'value' (which gets returned
> >> as-is if
> >> the collection is empty), and the return type of the closure. The
> >> following
> >> should work. I don't remember if I found a problem with it, or if
> >> Closure
> >> wasn’t generic by then. Probably the latter.
> >>
> >> public static<T, U extends T, V extends T> T inject(Collection
> >> self,
> >> U
> >> value, Closure<V> closure)
> >>
> >> Cheers,
> >> Peter
> >>
> >>
> >> --
> >> View this message in context:
> >> http://groovy.329449.n5.nabble.com/why-doesn-t-inject-return-a-generic-type-tp3377127p3377152.html
> >> Sent from the groovy - user mailing list archive at Nabble.com.
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe from this list, please visit:
> >>
> >> http://xircles.codehaus.org/manage_email
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> >
> >      http://xircles.codehaus.org/manage_email
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: why doesn't inject return a generic type?

Guillaume Laforge-4
In reply to this post by melix
Good point, Cédric.

Although in most common cases, inject() would return a value of the same type as the initial value, it's not necessarily always the case, and this example is a great counter-example. So I'd be cautious with such a generification which would restrict what users can do with inject().

Guillaume

On Wed, Feb 9, 2011 at 09:47, Cédric CHAMPEAU <[hidden email]> wrote:
In Groovy (and generally in a dynamically typed language), as far as I know, there's no reason why the return value should be the same type of the injected value. Here's a simple example :

(1..10).inject(1) { x,y -> x/y }

1 is an int, but the result of the injection will be a BigDecimal.

Cédric

Le 09/02/2011 07:58, Hamlet DArcy a écrit :
The inject method is declared as so: 

    public static Object inject(Collection self, Object value, Closure closure) {
        return inject(self.iterator(), value, closure);
    }

Why does this not use a generic type? 
This would be a better API, wouldn't it? 

    public static <T> T inject(Collection self, T value, Closure closure) {
        return inject(self.iterator(), value, closure);
    }

Is there a problem with that? 

--
Hamlet D'Arcy
[hidden email]


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





--

Cédric CHAMPEAU
Responsable du développement logiciel

>LINGWAY
  Des moteurs qui comprennent votre métier
  Immeuble Paritalie
  18-20 rue Pasteur
  94278 Le Kremlin-Bicêtre Cedex
  +33 (0)1 58 46 12 40




--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one
Reply | Threaded
Open this post in threaded view
|

Re: why doesn't inject return a generic type?

Peter Niederwieser
Guillaume Laforge wrote
Good point, Cédric.

Although in most common cases, inject() would return a value of the same
type as the initial value, it's not necessarily always the case
The signature that I gave covers this.

Cheers,
Peter
1234