Thread overview
what if return returned subclasses
Oct 20, 2021
Adam D Ruppe
Oct 20, 2021
Elronnd
Oct 20, 2021
Stefan Koch
Oct 20, 2021
Dennis
Oct 21, 2021
Adam Ruppe
Oct 21, 2021
12345swordy
Oct 21, 2021
Adam D Ruppe
Oct 21, 2021
H. S. Teoh
Oct 21, 2021
Adam Ruppe
October 20, 2021
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.
October 20, 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.
October 20, 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.

October 20, 2021

On Wednesday, 20 October 2021 at 22:42:43 UTC, Adam D Ruppe wrote:

>

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.

No, return doesn't imply that the parameter is returned. Even if the function is pure and there's only one parameter, it could still return a new object or a field of the parameter.

October 20, 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!
October 21, 2021
On Thursday, 21 October 2021 at 00:01:24 UTC, H. S. Teoh wrote:
> What would you do about this case?
>
> 	Object choice(return Object a, return Object b, int cond)

If there's two you'd have to do the common type.
October 21, 2021

On Wednesday, 20 October 2021 at 23:57:01 UTC, Dennis wrote:

>

No, return doesn't imply that the parameter is returned. Even if the function is pure and there's only one parameter, it could still return a new object or a field of the parameter.

Ah, I think I knew that but it didn't come to mind. Blah, I guess that kills it, at least kills reusing the syntax for it.

October 21, 2021

On Thursday, 21 October 2021 at 00:40:21 UTC, Adam Ruppe wrote:

>

On Wednesday, 20 October 2021 at 23:57:01 UTC, Dennis wrote:

>

No, return doesn't imply that the parameter is returned. Even if the function is pure and there's only one parameter, it could still return a new object or a field of the parameter.

Ah, I think I knew that but it didn't come to mind. Blah, I guess that kills it, at least kills reusing the syntax for it.

You want the ability to create new types?

-Alex

October 21, 2021
On Thursday, 21 October 2021 at 14:43:03 UTC, 12345swordy wrote:
> You want the ability to create new types?

No, it just returns the same thing you passed in. The function works on the interface/base class on the inside, but if the compiler knows it returns the same thing you passed in the argument, it can keep it as the child class on the outside.