Fix for broken upTo operator

53 views
Skip to first unread message

digulla

unread,
Oct 2, 2012, 4:30:27 AM10/2/12
to xtend...@googlegroups.com
I tried to fix the broken upTo operator using this code:

package com.avanon.blu.gen.xtext;

import java.util.Iterator;

public class IntRange implements Iterable<Integer> {

    private int min;
    private int max;
    private int start;
    private int end;

    public IntRange( int end ) {
        this( 0, end );
    }
    
    public IntRange( int start, int end ) {
        this.start = start;
        this.end = end;
        
        this.min = Math.min( start, end );
        this.max = Math.max( start, end );
    }
    
    @Override
    public Iterator<Integer> iterator() {
        return new IntIterator( start, end );
    }

    public static class IntIterator implements Iterator<Integer> {
        private int current;
        private final int end;
        private final int step;

        public IntIterator( int start, int end ) {
            this.current = start;
            this.end = end;
            this.step = ( start < end ) ? 1 : ( start == end ) ? 0 : -1;
        }
        
        @Override
        public boolean hasNext() {
            return current != end;
        }

        @Override
        public Integer next() {
            int result = current;
            current += step;
            return result;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();        
        }
    }
    
    public boolean contains( int value ) {
        return min <= value && value < max;
    }
    
    public static IntRange range( final Integer end ) {
        if (end == null) {
            throw new NullPointerException("a");
        }
        
        return new IntRange( end );
    }
    
    public static Iterable<Integer> operator_upTo(final Integer a, Number b) {
        if (a == null)
            throw new NullPointerException("a");
        return new IntRange( a, b.intValue() );
    }
}


But my method (last one in the code) isn't picked up. Why not?

Regards,

A. Digulla

Sebastian Zarnekow

unread,
Oct 8, 2012, 5:19:07 AM10/8/12
to xtend...@googlegroups.com
Hi Aaron,

sorry for the delayed response.

org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_upTo(int, int)

is defined for Int to int thus it is more specific than your own method. You'll have to overload your impl to make sure that it is called.

Regards,
Sebastian
signature.asc

digulla

unread,
Oct 10, 2012, 10:28:40 AM10/10/12
to xtend...@googlegroups.com
Am Montag, 8. Oktober 2012 11:18:54 UTC+2 schrieb Sebastian Zarnekow:

org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_upTo(int, int)

is defined for Int to int thus it is more specific than your own method. You'll have to overload your impl to make sure that it is called.

It seems the API changed in 2.3; my method signature is a copy&paste.

How do I overload a static method in Xtend?

Regards,

A. Digulla
Reply all
Reply to author
Forward
0 new messages