This is a nice illustration of the expression problem, which the article itself points out toward the end. Multiple dispatch is a language-level way to solve it. Crafting Interpreters [1] has a very approachable Java-based chapter that also covers these patterns in detail.
And std::variant is now a better choice for cases like in the article above, where previously inheritance was used to represent a closed set of possible subtypes.
7 comments
[ 1.8 ms ] story [ 42.5 ms ] threadOh, wait, that's me :-)
[0] https://en.wikipedia.org/wiki/Expression_problem
[1] https://craftinginterpreters.com/representing-code.html
And std::variant is now a better choice for cases like in the article above, where previously inheritance was used to represent a closed set of possible subtypes.
Just a quick note that this post is the first in a series: see https://eli.thegreenplace.net/tag/multiple-dispatch for the full series