what if return returned subclasses

Elronnd elronnd at elronnd.net
Wed Oct 20 23:21:07 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.

Doesn't go far enough imo.  Better to have full-on polymorphism:

T f(T: U)(T x, T y)

Is like:

U f(U x, U y)

Except that the return type is unified with both parameters.


More information about the Digitalmars-d mailing list