eclipse-archived/ceylon

infer type constructor args from given generic function references

Closed this issue · 1 comments

In #7291, I showed how to write a genericized version of compose(). However, it was of limited usefulness because you had to write in explicit type arguments every time you used it.

I have developed a pretty simple algorithm that does a very good, but not perfect, job of inferring type constructors from generic function refs passed as arguments to generic parameters.

For example, the following code now compiles:

<T> => F<G<T>>(T) compose<F,G>
        (<X>=>F<X>(X) f, <Y>=>G<Y>(Y) g)
        given F<X> given G<Y>
        => <U>(U u) => f(g(u));

T() lazy<T>(T t) => () => t;
[T] singleton<T>(T t) => [t];
    
value lazySingleton = compose(lazy, singleton);
[Integer]() int = lazySingleton1(4);
[String]() str = lazySingleton1("hello");

Which is amazingly cool.

Done!