loading...

re: Why self is not this in Python! VIEW POST

TOP OF THREAD FULL DISCUSSION
re: multiply dispatch, which in my opinion is not a good idea, especially when it comes to refactoring Can you please elaborate?

Sure, let me demonstrate:

Multiple dispatch

function (x:Person, y:Animal)
function (x:Person, y:Person)
function (x:Person, y:Person)
function (x:Animal, y:Animal)

Discriminated unions approach

function (x: Person | Animal, y: Person | Animal)

Those two are equivalent. The problem with the multiple dispatch occurs as soon as you try add one or n-more more parameters.

Adding an integer to DU function

function (x: Person | Animal, y: Person | Animal, z: Integer=0)

As you can see, I was able to add the parameter at one position, keeping the DRY principle.

Adding an integer to DU function

function (x:Person, y:Animal, z: Integer = 0)
function (x:Person, y:Person, z: Integer = 0)
function (x:Person, y:Person, z: Integer = 0)
function (x:Animal, y:Animal, z: Integer = 0)

This is a pure WET (We Enjoy Typing) principle in practice, at least in my opinion. And things get worse as soon as you try add more and more parameters.

This is going to be messy and utterly unsafe. Single dispatch limits this tendency and prevents you from possible troubles in the future.

To make simple dispatch less hairy you can leverage tuples to imitate multiple dispatch:

type LivingCreatures = (Animal, Person) | (Person | Animal) | ...

The function signature results in:

function (x: LivingCreatures, z: Integer=0)

To put it simply, I believe the multiple dispatch compromises a language safety and conciseness. But I may be wrong. :)

I never experienced this kind of pain and I'm using Next Generation Shell (with multiple dispatch) for years now.

function (x:Person, y:Animal, z: Integer = 0)
function (x:Person, y:Person, z: Integer = 0)
function (x:Person, y:Person, z: Integer = 0)
function (x:Animal, y:Animal, z: Integer = 0)

Any function that does use the z parameter - I don't see neither a problem with adding parameter nor a way around that. (Am I missing something here?).

Any function that does not use the z parameter - there is no need to add it, at least in NGS.

If NGS allows default arguments, then it may be OK. Julia, as far as I know, uses multiple dispatch and its users are happy. Recently, I run into a nasty problem with an overloaded method in C#, where I had to supply an argument I did not care for because the overloading did not cover my use case.

Still, I am not convinced that multiple dispatch is a good design for a modern language. But I may be wrong, of course. The language I have on my mind differs heavily from everything I've seen so far.

I took the example from wiki and thinking about it, even the example itself is crippled and code smell on its own. Actually, instead of using signature with types, much simpler and more correct would be just to rely on interfaces or traits.

So instead

function(x: Animal, y:Person, z: integer)
...

Would be just one

function(x: CommonTraitForAnimalAndPerson, y: CommonTraitForAnimalAndPerson, z: integer)

This one is actually more concise and general than any previous solution.

code of conduct - report abuse