You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Stanislav Poslavsky (JIRA)" <ji...@apache.org> on 2016/06/20 11:00:09 UTC

[jira] [Created] (GROOVY-7869) Groovy category does now work with abstract iterable classes

Stanislav Poslavsky created GROOVY-7869:
-------------------------------------------

             Summary: 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.7, 2.4.6, 2.4.5, 2.4.4, 2.4.3, 2.4.2, 2.4.1, 2.4.0
         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)