You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by Peter Ledbrook <pe...@cacoethes.co.uk> on 2015/10/09 09:54:15 UTC

Type safety on sum()/getAt()

Hi,

I've noticed that @TypeChecked doesn't work so well with typed collections
and the sum() & getAt() methods (perhaps others as well). I had a quick
look at sum() in DefaultGroovyMethods and noticed that it doesn't use
parameterised types, unlike inject() for example.

Would there be any issues with updating sum() and getAt() to work with
parameterised types? The current situation makes using @TypeChecked harder
than it needs to be.

Thanks,

Peter

--
Peter Ledbrook
t: @pledbrook
w: http://www.cacoethes.co.uk/

Re: Type safety on sum()/getAt()

Posted by Peter Ledbrook <pe...@cacoethes.co.uk>.
>
> I don't think it is possible without a breaking change. Today `sum` and
> `getAt` can return anything. There's no requirement that the sum of A+A is
> an A, nor that A+B return type is the same as B+A, or any kind of
> constraint like that. Adding this at the source level would enforce type
> safety, but it would also break existing code.
>

That's a shame, but understandable. Would it be something to consider for
Groovy 3 though? It seems strange that inject() does have the requisite
type information but sum() does not. The fewer the inconsistencies the
better.

Cheers,

Peter

--
Peter Ledbrook
t: @pledbrook
w: http://www.cacoethes.co.uk/

Re: Type safety on sum()/getAt()

Posted by Cédric Champeau <ce...@gmail.com>.
I don't think it is possible without a breaking change. Today `sum` and
`getAt` can return anything. There's no requirement that the sum of A+A is
an A, nor that A+B return type is the same as B+A, or any kind of
constraint like that. Adding this at the source level would enforce type
safety, but it would also break existing code.

2015-10-09 9:54 GMT+02:00 Peter Ledbrook <pe...@cacoethes.co.uk>:

> Hi,
>
> I've noticed that @TypeChecked doesn't work so well with typed collections
> and the sum() & getAt() methods (perhaps others as well). I had a quick
> look at sum() in DefaultGroovyMethods and noticed that it doesn't use
> parameterised types, unlike inject() for example.
>
> Would there be any issues with updating sum() and getAt() to work with
> parameterised types? The current situation makes using @TypeChecked harder
> than it needs to be.
>
> Thanks,
>
> Peter
>
> --
> Peter Ledbrook
> t: @pledbrook
> w: http://www.cacoethes.co.uk/
>

Re: Type safety on sum()/getAt()

Posted by Pascal Schumacher <pa...@gmx.net>.
Hi Peter,

no idea if there are any special issues with updating sum() and getAt(), 
but in general there is room for improvement of parameterised types in 
DefaultGroovyMethods etc. E.g. this pull request: 
https://github.com/groovy/groovy-core/pull/610/files adds parameterised 
types to a lot of DefaultGroovyMethods and IOGroovyMethods but it was 
never merged due to lack of tests.

-Pascal

Am 09.10.2015 um 09:54 schrieb Peter Ledbrook:
> Hi,
>
> I've noticed that @TypeChecked doesn't work so well with typed 
> collections and the sum() & getAt() methods (perhaps others as well). 
> I had a quick look at sum() in DefaultGroovyMethods and noticed that 
> it doesn't use parameterised types, unlike inject() for example.
>
> Would there be any issues with updating sum() and getAt() to work with 
> parameterised types? The current situation makes using @TypeChecked 
> harder than it needs to be.
>
> Thanks,
>
> Peter
>
> --
> Peter Ledbrook
> t: @pledbrook
> w: http://www.cacoethes.co.uk/