| |
| Posted by H. S. Teoh in reply to Adam D Ruppe | PermalinkReply |
|
H. S. Teoh
Posted in reply to Adam D Ruppe
| 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!
|