www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - what if return returned subclasses

reply Adam D Ruppe <destructionator gmail.com> writes:
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.
Oct 20 2021
next sibling parent Elronnd <elronnd elronnd.net> writes:
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.
Oct 20 2021
prev sibling next sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
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.
Oct 20 2021
prev sibling next sibling parent reply Dennis <dkorpel gmail.com> writes:
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.
Oct 20 2021
parent reply Adam Ruppe <destructionator gmail.com> writes:
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.
Oct 20 2021
parent reply 12345swordy <alexanderheistermann gmail.com> writes:
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
Oct 21 2021
parent Adam D Ruppe <destructionator gmail.com> writes:
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.
Oct 21 2021
prev sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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!
Oct 20 2021
parent Adam Ruppe <destructionator gmail.com> writes:
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.
Oct 20 2021