Hacker News new | ask | show | jobs
by rileyphone 908 days ago
Is there anything you need multimethods for that can't be patched with visitors and other design patterns? They have always seemed to me like a neat feature that are devilishly tricky to implement and difficult to reason about for the average programmer.
1 comments

You don't need multimethods per se, but you need something and `dynamic_cast` is usually easiest (and with reasonable restrictions, most efficient).

Overloaded operators is a major category of problem here. The "which subclass (if any) is more derived" might be done by the compiler proper, but that still needs to use the cast internally. And of course if you ignore operator overloading, you're just pulling a Java and mandating extra verbosity; the user's problem still has to be solved the exact same way.

(most other use cases for multimethods I don't find compelling)

Successful dynamic_casts are fast. But failing dynamic_casts usually involve at least one strcmp and are extremely expensive.

https://gcc.gnu.org/legacy-ml/gcc-patches/2009-07/msg01239.h...

For the particular set of tradeoffs made by C++. When rolling your own you don't have to do make the same choices.