groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Stanislav Poslavsky (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (GROOVY-7869) Groovy category does now work with abstract iterable classes
Date Mon, 20 Jun 2016 11:03:05 GMT

    [ https://issues.apache.org/jira/browse/GROOVY-7869?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15339346#comment-15339346
] 

Stanislav Poslavsky commented on GROOVY-7869:
---------------------------------------------

I will very appreciate any workaround, since this bug breaks my DSL and I can't force the
users to use old Groovy versions.

> Groovy category does now work with abstract iterable classes
> ------------------------------------------------------------
>
>                 Key: GROOVY-7869
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7869
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 2.4.0, 2.4.1, 2.4.2, 2.4.3, 2.4.4, 2.4.5, 2.4.6, 2.4.7
>         Environment: OS X/Linux/Windows
>            Reporter: Stanislav Poslavsky
>            Priority: Critical
>
> If one has a simple abstract iterable class and want to overload *plus* operator using
category, Groovy will use different implementation of 'plus' from that defined in the category.
> Abstract iterable class:
> {code:java}
> import java.util.Iterator;
> public abstract class MyAbstractClass implements Iterable<Number> {
>     public final int num;
>     public MyAbstractClass(int num) { this.num = num; }
>     @Override
>     public String toString() { return String.valueOf(num); }
>     @Override
>     public Iterator<Number> iterator() {
>         //just some dummy empty iterator
>         return new Iterator<Number>() {
>             @Override
>             public boolean hasNext() { return false; }
>             @Override
>             public Number next() { return null; }
>         };
>     }
> }
> {code}
> A single implementation:
> {code:java}
> public class MyAbstractClassImpl extends MyAbstractClass {
>     public MyAbstractClassImpl(int a) { super(a); }
> }
> {code}
> Category that overloads *plus* operator:
> {code:java}
> class MyCategory {
>     public static MyAbstractClass plus(MyAbstractClass a, MyAbstractClass b) {
>         return new MyAbstractClassImpl(a.num + b.num);
>     }
> }
> {code}
> Test case:
> {code:java}
> use(MyCategory) {
>     def a = new MyAbstractClassImpl(1)
>     def b = new MyAbstractClassImpl(2)
>     println a + b //expected 3, but received []
>     assert (a+b).num == 3 // so here we see exception
> }
> {code}
> Expected to see *3*, but instead Groovy returns empty list, since it uses plus operator
defined for iterable, but not that is defined in MyCategory. 
> There is no bug in version 2.2.x.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message