2 ms·
I am really interested to know why I would want such a thing. In years of programming, I have never needed a heterogeneous collection except in a dynamically ty
by evincarofautumn 12y ago
I am really interested to know why I would want such a thing. In years of programming, I have never needed a heterogeneous collection except in a dynamically typed language where I can’t express more precise types. My values are always related by some set of operations I want to perform on them.
In C++, for example, I might have a “vector<unique_ptr<GameObject>>” containing instances of various subclasses of “GameObject” such as “Player” or “Enemy”. “GameObject” has some virtual member functions such as “update()” and “render()”.
In Haskell I can do exactly the same thing with a “GameObject” type class containing the “update” and “render” methods, and implement “GameObject” for my “Player” and “Enemy” types.
In Python I do a dynamic check on every value in the collection, on every iteration, to see if “update” and “render” are present and callable. It just seems wasteful because you know that information ahead of time—a non–game object should never end up in that collection, so why waste brain power and computing power considering the possibility?
There must be some advantage I’m not seeing here.