Record with keyword used with inheritance seems to replace the reference with it's own #8001
Unanswered
bogdan-patraucean
asked this question in
Q&A
Replies: 2 comments 11 replies
-
You're using |
Beta Was this translation helpful? Give feedback.
8 replies
-
I confirm that adding another parameter to differentiate itself from the copy constructor of the record achieves what I want. Thanks @HaloFour for pointing this out, an updated fiddle can be found here. Tho, a question still remains: do we want the copy constructor to be overriden so easely and by mistake? Is there nothing we can do to prevent that? |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I think I discovered an issue with how records work in combination with inheritance. Updating an object and making a copy of it using the
with
keyword will replace the old reference of the base class with it's own (the derived class).To showcase this we need 3 classes, and as example (fiddle here) I chose state management:
GenericState
class to define what a state isState
class where we will store all our dataSubstate
class that's part of theState
(which is the root/parent)And here is what I do, I create a state with a child, and I specify the parent to the child so it always knows who the parent is. The state will not have any parent so I initialize it with null since it's the root. When I try to update the state by updating the child's property to 4 the reference to the parent will be replaced with the reference of the child.
So, the first log to console will display
Parent
, while the second will displayChild
, tho,with
should copy everything as is.Am I doing something wrong? For me this seems like an issue, as I would expect the reference to be kept, not replaced.
Note
The same example without the abstract record class works as expected (fiddle here).
Beta Was this translation helpful? Give feedback.
All reactions