Accept inheriting class when parent class is used in Array type hinting #12828
tyson-nw
started this conversation in
Engine Core
Replies: 1 comment
-
This is actually quite an interesting problem and it's not super easy to solve. Since the type is stored in the object and not in the variable, you could consider a situation like this: var array1: Array[Child] = ...
var array2: Array[Parent] = array1
# the object in array2 still only accepts Child types and "smaller"
# but array2 itself suggests we can add parents to it
array2.append(Parent.new())
# static code analysis says this is okay,
# but you get a runtime error from this More information: https://en.m.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the project you are working on:
I have a generic class called Token with two child classes, AntagonistToken and ProtagonistToken. I would like to be able to pass an array of a mix of tokens in an array to a function with Type hinting
Describe the problem or limitation you are having in your project:
Currently type hinting of Array content requires the passed objects to specifically be of the class listed. You cannot pass an aray of objects of a child classes to the function.
Show a mock up screenshots/video or a flow diagram explaining how your proposal will work:
You would be allowed to pass a mix of objects that all have the same base class to a function that is type hinted.
Beta Was this translation helpful? Give feedback.
All reactions