<p>Years ago, I have implemented something in C++ that involves two kinds of list of the same value type: a sparse list and a dense list. Each operation between two lists has 4 versions (two of them are the same, with the parameter order reversed). Both kinds of list are the subclass of the same base class. And I used a lot of virtual functions, to make other code just see them as the general list. Naturally, I should implement the 4 versions of an operation between lists as virtual functions. But I was only able to do that using some hacky way, by manually comparing the type of the parameter and doing some force casting.</p>
<p>For example, let's say there are two classes <code>String</code> and <code>Integer</code>, and I want to compute some kind of <code>mix</code> function. There are four possible cases:</p>
<pre><code>mix    |  &quot;abc&quot;      10
-------|----------------------
&quot;def&quot;  |  &quot;abcdef&quot;   &quot;def10&quot;
2      |  &quot;abcabc&quot;    20
</code></pre>
<p>In my <code>Integer</code> and <code>String</code> classes, I have to define <code>mix</code> methods that dispatch on the type of their argument (ie. a string can mix with another string or an integer, and same for an integer object).</p>
<p>So, what's a possible good grammar of doing this? Alternatively, are there already languages with grammars that could let people do this more easily and intuitively?</p>
<p>I consider something at least not involving force casting acceptable. But better answers could have better syntactic sugars.</p>
<p>It could be easily done in some functional languages with parameter pattern matching, but I'm specifically thinking about virtual functions and inheritance in an object oriented language. Ideas from functional languages may help, only if they support inheritance.</p>

<object-oriented><polymorphism><dynamic-dispatch>
Virtual functions from two classes
CC BY-SA 4.0
2023-07-03T13:57:11.447Z
Typically if you have a fixed set of representations, it's better for performance to avoid dynamic dispatch and instead use a [discriminated union](https://en.wikipedia.org/wiki/Tagged_union). Many modern languages support these, particularly ML-family languages, which are popular for compiler development. In Rust, this would be something like `enum List { Sparse(...), Dense(...), }` and then you would use `match my_list { Sparse(...) => ..., Dense(...) => ..., }` to implement the list functions. I'm not sure if this is the kind of answer you're looking for, though, since you seem to be ...
kaya3
CC BY-SA 4.0
2023-07-03T13:57:42.410Z
... asking both how to implement this and also how to design a language which makes it easy to implement this.
kaya3
CC BY-SA 4.0
2023-07-03T14:11:42.010Z
@kaya3-supportthestrike It could be useful, as it apparently doesn't involve force casting. But I hope there could be ways as easy as virtual functions, and supporting inheritance (i.e. not in a fixed set). I'm also asking how to implement in C++ just in case I missed something obvious.
user23013
CC BY-SA 4.0
2023-08-09T15:50:14.010Z
Strangely, nobody mentionned Julia. It is the most mainstream language to implement dynamic multiple dispatch. And some people believe it to be the best feature of the language that sets it apart. Reference for Julia multiple dispatch: https://docs.julialang.org/en/v1/manual/methods/
Jean-Armand Moroni
<p>This feels more like a stackoverflow Q than a PLDI one as you seem to be talking about how to use a feature not realize it in a language.
I will assume you mean the latter.</p>
<p>If I've interpreted your question correctly, what you are looking for is called <a href="https://en.wikipedia.org/wiki/Multiple_dispatch" rel="nofollow noreferrer">multiple dispatch</a> or more rarely <em>double dispatch</em>
Once you know the name for this concept you can find plenty of information.
Pattern matching is one way to solve this problem.</p>
<p>Its a pain to do in C++ but there are solutions see for example:</p>
<ul>
<li><a href="https://stackoverflow.com/questions/1749534/multiple-dispatch-in-c">https://stackoverflow.com/questions/1749534/multiple-dispatch-in-c</a></li>
<li><a href="https://dzone.com/articles/double-dispatch-in-c" rel="nofollow noreferrer">https://dzone.com/articles/double-dispatch-in-c</a></li>
</ul>
<p>There are languages that support it natively. My canonical example is <a href="https://en.wikipedia.org/wiki/Dylan_(programming_language)" rel="nofollow noreferrer">dylan</a>.</p>
<p>And there are proposals for including pattern matching in a future variant of C++ see for example <a href="https://wg21.link/P1371" rel="nofollow noreferrer">https://wg21.link/P1371</a></p>
<p>There are also library solutions such as <a href="https://github.com/mpark/patterns" rel="nofollow noreferrer">https://github.com/mpark/patterns</a></p>

CC BY-SA 4.0
2023-07-03T15:05:35.293Z
2023-07-03T15:46:10.953Z
2023-07-03T15:46:10.953Z
Bruce Adams
CC BY-SA 4.0
2023-07-03T23:46:48.260Z
Yes, multiple dispatch. How could I fix the question? Maybe "what are the options for multiple dispatch syntax"? I should have asked ChatGPT for the term in hindsight.
user23013
# Virtual functions from two classes
Posted by: username_0 on 2023-07-03
Years ago, I have implemented something in C++ that involves two kinds of list of the same value type: a sparse list and a dense list. Each operation between two lists has 4 versions (two of them are the same, with the parameter order reversed). Both kinds of list are the subclass of the same base class. And I used a lot of virtual functions, to make other code just see them as the general list. Naturally, I should implement the 4 versions of an operation between lists as virtual functions. But I was only able to do that using some hacky way, by manually comparing the type of the parameter and doing some force casting.
For example, let's say there are two classes String and Integer, and I want to compute some kind of mix function. There are four possible cases:
mix    |  "abc"      10
-------|----------------------
"def"  |  "abcdef"   "def10"
2      |  "abcabc"    20

In my Integer and String classes, I have to define mix methods that dispatch on the type of their argument (ie. a string can mix with another string or an integer, and same for an integer object).
So, what's a possible good grammar of doing this? Alternatively, are there already languages with grammars that could let people do this more easily and intuitively?
I consider something at least not involving force casting acceptable. But better answers could have better syntactic sugars.
It could be easily done in some functional languages with parameter pattern matching, but I'm specifically thinking about virtual functions and inheritance in an object oriented language. Ideas from functional languages may help, only if they support inheritance.
**Tags:** object-oriented polymorphism dynamic-dispatch
> **Comments:**
> **username_1**: Typically if you have a fixed set of representations, it's better for performance to avoid dynamic dispatch and instead use a [discriminated union](https://en.wikipedia.org/wiki/Tagged_union). Many modern languages support these, particularly ML-family languages, which are popular for compiler development. In Rust, this would be something like `enum List { Sparse(...), Dense(...), }` and then you would use `match my_list { Sparse(...) => ..., Dense(...) => ..., }` to implement the list functions. I'm not sure if this is the kind of answer you're looking for, though, since you seem to be ...  
> **username_1**: ... asking both how to implement this and also how to design a language which makes it easy to implement this.  
> **username_0**: @kaya3-supportthestrike It could be useful, as it apparently doesn't involve force casting. But I hope there could be ways as easy as virtual functions, and supporting inheritance (i.e. not in a fixed set). I'm also asking how to implement in C++ just in case I missed something obvious.  
> **username_2**: Strangely, nobody mentionned Julia. It is the most mainstream language to implement dynamic multiple dispatch. And some people believe it to be the best feature of the language that sets it apart. Reference for Julia multiple dispatch: https://docs.julialang.org/en/v1/manual/methods/  
---
## 1 answers
### Answer by username_3 on 2023-07-03. Score: 4 | Accepted answer
This feels more like a stackoverflow Q than a PLDI one as you seem to be talking about how to use a feature not realize it in a language.
I will assume you mean the latter.

If I've interpreted your question correctly, what you are looking for is called [multiple dispatch](https://en.wikipedia.org/wiki/Multiple_dispatch) or more rarely *double dispatch*
Once you know the name for this concept you can find plenty of information.
Pattern matching is one way to solve this problem.

Its a pain to do in C++ but there are solutions see for example:

* <https://stackoverflow.com/questions/1749534/multiple-dispatch-in-c>
* <https://dzone.com/articles/double-dispatch-in-c>

There are languages that support it natively. My canonical example is [dylan](https://en.wikipedia.org/wiki/Dylan_(programming_language)).

And there are proposals for including pattern matching in a future variant of C++ see for example <https://wg21.link/P1371>

There are also library solutions such as <https://github.com/mpark/patterns>
> **Comments:**
> **username_0** (2023-07-03): Yes, multiple dispatch. How could I fix the question? Maybe "what are the options for multiple dispatch syntax"? I should have asked ChatGPT for the term in hindsight.  
---
