class base {}
class derived : base {}
class unrelated {}
void fillInProperType(ref@ obj, base@& out b, unrelated@& out u)
{
if ( obj is null )
{
print("obj is null");
return;
}
@b = cast<base>(obj);
@u = cast<unrelated>(obj);
if ( b !is null )
print("b");
else if ( u !is null )
print("u");
else
print("both are null");
}
void workaround(ref@ obj, base@& out b, unrelated@& out u)
{
if ( obj is null )
{
print("obj is null");
return;
}
base@ _b = cast<base>(obj);
unrelated@ _u = cast<unrelated>(obj);
if ( _b !is null )
{
print("b");
@b = _b;
}
else if ( u !is null )
{
print("u");
@u = _u;
}
else
print("both are null");
}
void main()
{
/*
ref@ r = null;
base@ b = null;
unrelated@ u = null;
fillInProperType(r, b, u); // "obj is null" [CORRECT]
*/
/*
ref@ r = base();
base@ b = null;
unrelated@ u = null;
fillInProperType(r, b, u); // "b" [CORRECT]
*/
/*
ref@ r = unrelated();
base@ b = null;
unrelated@ u = null;
fillInProperType(r, b, u); // "b" [INCORRECT]
*/
/*
ref@ r = derived();
base@ b = null;
unrelated@ u = null;
fillInProperType(r, b, u); // "b" [CORRECT, but probably by accident]
*/
/*
ref@ r = base();
base@ b = null;
unrelated@ u = null;
workaround(r, b, u); // "b" [CORRECT]
*/
/*
ref@ r = unrelated();
base@ b = null;
unrelated@ u = null;
workaround(r, b, u); // "u" [CORRECT]
*/
ref@ r = derived();
base@ b = null;
unrelated@ u = null;
workaround(r, b, u); // "b" [CORRECT]
}
fillInProperType() is assigning directly to an out reference to a handle and doesn't seem to work correctly. workaround() first assigns to local variables.. and that seems to work correctly.