[ https://issues.apache.org/jira/browse/GROOVY-7869?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15344084#comment-15344084 ]
Stanislav Poslavsky commented on GROOVY-7869: --------------------------------------------- Great! Many thanks, this workaround works! For the future: should I consider this behaviour (interface has more preference) as a bug or as a feature (since in Groovy 2.2.x behaviour is different)? > 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: Blocker > > 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)