I have the code:
shared Integer getInt() {
return 42;
}
shared Integer() i1() {
return getInt;
}
shared Callable<Integer,Nothing> i2() {
return getInt;
}
shared Callable<Integer,[]> i3() {
return getInt;
}
void run() {
// OK
i1()();
// Illegal `[] is not assignable to Nothing`
i2()();
// OK
i3()();
}
I am not sure why the compiler is OK with the "i2" declaration though. Nothing is a subtype of everything and therefore a subtype of the empty tuple, so I can kind of understand why I can do the declaration. But once I've done that it seems impossible for me to invoke "i2" normally as calling it with no arguments, empty tuple, means calling it with a supertype of what it wants which Ceylon rejects. So is it possible at all to invoke the getInt
returned from i2?
Lets modify your example a little bit.
As you mentioned
Nothing
is subtype of[]
, because it's subtype of everything. It is also subtype of[Integer]
.Type
Callable<Integer, Nothing>
describes any function that returnsInteger
. However it doesn't say it takes no arguments.It is possible to call function function returned from
i2
, but you need to typecheck it first:Above example is fine, because
[Integer, Integer]
is assignable to[Integer]
and thereforeCallable<Integer, [Integer]>
is assignable toCallable<Integer, [Integer, Integer]>
, as the second type parameter ofCallable
is contravariant.