weird problem with generics and type inference

Topics: General, Language Specification
May 5, 2014 at 2:22 PM
Edited May 5, 2014 at 4:02 PM
There is a error in the code below with the lots of lines of text. When I change the Y parameter of the generic in the arrow marked line to the type such as number or string all is ok, no error after compilation

Question 1 - is it a compiler bug ?
Question 2 - how to fix the error ?
class channel<S,Y>{
     
    public merge(...channels:channel<any, Y>[]) {

        var ch = new channel<void, channel<any, Y>>(); // <---

        var result = ch.flatMap(x => x);

        return result;
    }
     
    public flatMap<R>(projector: (data: Y) => channel<any, R>): channel<Y, R> {
        return null;
    }
 }
expected type of result is channel<any, Y>, but the compiler infers something different
May 5, 2014 at 3:43 PM
Edited May 5, 2014 at 3:43 PM
The error message

Error 1 Supplied parameters do not match any signature of call target:
Call signatures of types '(x: channel<any, channel<any, Y>>) => channel<any, channel<any, Y>>' and 
                         '(data: channel<any, channel<any, Y>>) => channel<any, Y>' are incompatible:
    Types 'channel<any, channel<any, Y>>' and 'channel<any, Y>' originating in infinitely expanding type reference have incompatible type arguments.    
To me looks like it a compiler bug, but I'm not sure
Coordinator
May 5, 2014 at 5:27 PM
May 5, 2014 at 7:06 PM
Edited May 6, 2014 at 5:37 PM
Thanks !

I found better description of the problem
class channel<S,Y> {

static flatMapImpl<T,R>(self: channel<any, T>, projector:(t:T) => channel<T,R>) {
    var ch = new channel<T, R>();

    return ch;  
}

public flatMap<R>(projector: (data: Y) => channel<Y, R>) {
    return channel.flatMapImpl(this, projector);
}

public merge2(...targets: channel<any, Y>[]) {
    var ch = new channel<void, channel<any, Y>>();
    var r = channel.flatMapImpl(ch, x => x);

    return r;
}

public merge(...targets: channel<any, Y>[]) {
    var ch = new channel<void, channel<any, Y>>();
    var result = ch.flatMap(x => x); // <-- compilation error

    return result;
}
}
The question is - why merge written via member function can't be compiled and how to fix it ?

Of course, I can set some annotation such as <any>, but, actually, the member function is the main use case,. That means that I will have to put annotations or etc. to every call.