Thanks,
Sven
it's currently not possible to use the type information of a cast expression. Please consider the following scenario:
def <T extends Integer> cannotUseCastContext() {
[T a,T b|a+b] as (T,T)=>T
}
a + b will return an Integer / int but the cast would imply that 'T' has to be returned. Unfortunately, Integer is not a T.
The equivalent Java code won't compile either:
public <T extends Integer> Function<? super T, ? extends T> similarThingInJava() {
return new Function<T, T>() {
public T apply(T input) {
return Integer.valueOf(input + input);
}
};
}
Type mismatch: cannot convert from Integer to T
You'd have to cast the result explicitly to T or use an unsafe cast like
return (Function<? super T, ? extends T>) new Function<T, Integer>() {
public Integer apply(T input) {
return Integer.valueOf(input + input);
}
};
which is basically what the previous Xtend example does.
Nevertheless, please feel free to file a ticket.
Regards,
Sebastian
[T a,T b|a+b] as (T,T)=>T
var Function<Integer, Integer> s = [x|x * 2]
var s = [x|x * 2] as Function<Integer,Integer>
Multiple markers at this line- There is no context to infer the closure's argument types from.Consider typing the arguments or put the closures into a typedcontext.- Couldn't resolve reference to JvmIdentifiableElement '*'.
public abstract class SimpleSelectionAdapter extends SelectionAdapter {public abstract void widgetSelected(SelectionEvent e);}
Set<String> strings
static (int,int)=>int min = [i,j | if (i < j) i else j]
def go() {
strings.map[index].reduce(min)
}
def index(String s) {
s.hashCode
}
Set<String> strings
static (int,int)=>int min = [i,j | if (i < j) i else j]
static (String)=>int index = [s | s.hashCode]
def go() {
// Incompatible receiver type.
// Expected java.lang.Iterable<java.lang.Integer>
// or java.lang.Integer[] or int[]
// but was java.lang.Iterable<? extends java.lang.Integer>
strings.map(index).reduce(min)
}
Daniel