what if return returned subclasses

Stefan Koch uplink.coder at googlemail.com
Wed Oct 20 23:48:57 UTC 2021

On Wednesday, 20 October 2021 at 22:42:43 UTC, Adam D Ruppe 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.

that creates a polymorphic expression.
In other words I'd be the same as
T identity(T : Object) { return o; }

I am assuming you intend for this to not create new functions 
open being instantiated.

That would work in this simple case but as soon as this is used 
in compositional ways. It will most likely break.

More information about the Digitalmars-d mailing list