Am Fri, 29 May 2015 16:09:12 +0200
schrieb Gavin King <
gavin...@gmail.com>:
I removed the union type now and added Base<out Self> to the definition
of Base. Now I can't use use-site variance and have a working process()
function, but a compilation error in deserialize().
If I write Base<in Self>, then the deserialize() works, but the
process() yields a compilation error.
Should I try use-site variance with an newer ceylon version than 1.1?
I haven't switched to development builds as I want to use the IDE. Is
the development IDE built with recent versions of ceylon?
I now have the following:
----------------------
abstract class Base<out Self>() of Self
given Self satisfies Base<Self>
{shared variable Integer a = 0;}
class Sub1() extends Base<Sub1>(){shared variable Integer b = 0;}
class Sub2() extends Base<Sub2>(){shared variable Integer b = 0;}
Base<Nothing> deserialize(String s)
{
switch (s)
case ("sub1") {
Sub1 r = Sub1();
return r; // error if class Base<out Self>, "in" works
}
case ("sub2") {
Sub2 r = Sub2();
return r; // error if class Base<out Self>, "in" works
}
else {
throw Exception("invalid input data");
}
}
void process<X>(X input)
given X satisfies Base<X>
{
switch (input)
case (is Sub1) {
// process Sub1
}
case (is Sub2) {
// process Sub2
}
else {
throw Exception("unsupported input class");
}
}
void run()
{
String sx = "sub2";
Base<Nothing> inputX = deserialize(sx);
// some generic processing on Base
print(inputX.a.string);
process(inputX); // erorr if class Base<in Self>, "out" works
}
----------------------
Thanks, Dirk