digitalmars.D - what if return returned subclasses
- Adam D Ruppe (10/10) Oct 20 2021 ok consider this:
- Elronnd (6/16) Oct 20 2021 Doesn't go far enough imo. Better to have full-on polymorphism:
- Stefan Koch (8/18) Oct 20 2021 that creates a polymorphic expression.
- Dennis (4/8) Oct 20 2021 No, `return` doesn't imply that the parameter is returned. Even
- Adam Ruppe (3/6) Oct 20 2021 Ah, I think I knew that but it didn't come to mind. Blah, I guess
- 12345swordy (3/9) Oct 21 2021 You want the ability to create new types?
- Adam D Ruppe (5/6) Oct 21 2021 No, it just returns the same thing you passed in. The function
- H. S. Teoh (12/28) Oct 20 2021 What would you do about this case?
- Adam Ruppe (2/4) Oct 20 2021 If there's two you'd have to do the common type.
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
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
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
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
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
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:You want the ability to create new types? -AlexNo, `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 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.
Oct 21 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!
Oct 20 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.
Oct 20 2021