Why no multiple-dispatch?

Idan Arye via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 24 17:42:39 PDT 2014


On Monday, 25 August 2014 at 00:08:25 UTC, Jonathan M Davis wrote:
> On Sunday, 24 August 2014 at 23:42:51 UTC, Aerolite wrote:
>> Hey all,
>>
>> I was surprised to learn yesterday that D does not actually
>> support Multiple-Dispatch, also known as Multimethods. Why is
>> this? Support for this feature is already present in Scala, C#
>> 4.0, Groovy, Clojure, etc... Would it not make sense for D to
>> remain competitive in this regard?
>>
>> While I think many of us are aware that problems of the nature
>> that require Multiple-Dispatch can be approached with the 
>> Visitor
>> Pattern, there seems to be a general consensus that the Visitor
>> Pattern is pretty cumbersome and boilerplate-heavy, and thus
>> should be avoided.
>>
>> The common response from my searching around seems to be that a
>> template-based, static implementation of Multiple-Dispatch is 
>> the
>> go-to solution in D, but considering the existing 
>> template-bloat
>> issues we have, I can't help but wonder if language support for
>> this feature might be a better path to go down. Seems like it
>> wouldn't be too difficult to implement, although I've not 
>> looked
>> very deeply into dmd's source-code.
>>
>> So what seems to be the situation here?
>
> At this point, if something can be implemented in a library 
> rather than in the language, the odds are low that it will be 
> solved in the language. The language is very powerful and 
> already a bit complicated, so usually the response for 
> questions like this is that we'll take advantage of D's 
> existing features to implement the new feature rather than 
> complicating the language further. If you could come up with a 
> very good reason why it had to be in the language, then maybe 
> it would happen, but my guess is that that's not likely to 
> happen.
>
> - Jonathan M Davis

Speaking of library solutions, I checked with my `castSwitch` PR 
and it managed to implement single-argument multi-dispatch:

https://github.com/D-Programming-Language/phobos/pull/1266#issuecomment-53217374

I'll try to get it to support multiple arguments(shouldn't be 
that hard) and then all we'll need is a simple wrapper to have 
our very own library implemented single dispatch.

I'm thinking we'll want two versions - a regular templated 
function like in Vladimir's reply, and also a template mixin that 
can take a bunch of methods with the same name(or annotated with 
a UDA) and create a multi-method of them.

These wrappers will be another PR, but they can be based on 
`castSwitch` for it's somewhat-optimized type-comparison 
implementation.


More information about the Digitalmars-d-learn mailing list