what if return returned subclasses
H. S. Teoh
hsteoh at quickfur.ath.cx
Thu Oct 21 00:01:24 UTC 2021
On Wed, Oct 20, 2021 at 10:42:43PM +0000, Adam D Ruppe via Digitalmars-d wrote:
> ok consider this:
>
> Object identity(return Object o) { return o; }
>
>
> Subclass s = identity(new Subclass());
>
>
> That is, it is converted to the base class before passed to the
> function, but since it is marked `return` anyway, the compiler knows
> it is the same instance, so the static type passed in is retained in
> the return value. So it is kinda like type erased but then repainted
> back on.
>
> Would this break anything? There's a few OOP patterns that might be
> made a lil nicer if this just worked.
What would you do about this case?
Object choice(return Object a, return Object b, int cond)
{
return (cond) ? a : b;
}
SubclassA a;
SubclassB b;
a = choice(a, b, 1); // what if you assigned to b instead, or cond == 0?
T
--
Turning your clock 15 minutes ahead won't cure lateness---you're just making time go faster!
More information about the Digitalmars-d
mailing list