Open ivan-mogilko opened 1 year ago
There is also the python approach of using super. It would be something like this.super()
would give the reference for that object as its parent class whatever that is - but this would be tricky with the type system we have in place.
Overall, I like the C# style best, as it doesn't automatically invite other types of casting as a complexity.
There is also the python approach of using super. It would be something like
this.super()
would give the reference for that object as its parent class whatever that is - but this would be tricky with the type system we have in place.
Hmm, but that upcasts as opposed of downcast? AGS already supports getting parent's pointer, you can do:
GUIControl *control = myButton;
EDIT: I think I forgot to mention another variant, idk how good it is, but worth mentioning: a static method that casts to this type from parent, like:
child_ptr = ChildType.CastFrom(parent_ptr);
I got this idea while thinking that GUIControl.AsButton, AsLabel etc is bad, because it hardcodes the list of descendants, so I wondered if we could instead have
Button.FromControl(control_ptr)
or similar.
CC @fernewelten
After RTTI was added in ags4, it is now theoretically possible to find out the managed object's parent(s), knowing its type. This opens a possibility to support downcasting, that is - a conversion from the base type pointer to a child type pointer. As opposed to the upcasting, which may be calculated at compile time, downcasting has to be performed at runtime, as compiler cannot know which type will be stored in the base pointer.
What would be required for this?
Child* child_ptr = (Child*)parent_ptr
;Child* child_ptr = parent_ptr as Child
;cast<T>(parent_ptr)
orcast<T*>(parent_ptr)
, but it looks more complicated.