News for hackers. Stuff that matters.
Re:What can we learn from how compilers are designed? by ZephyrP on Friday March 16, 2018 @08:12PM (#16604908)
Multiple (virtual) dispatch is a central feature of C++ (and I suspect Java as well).
Re:What can we learn from how compilers are designed? by groovy2shoes on Friday March 16, 2018 @08:33PM (#16604980)
Virtual dispatch in C++ and Java is dynamic dispatch, but it is singly so rather than multiply so as found in CLOS, Dylan, Julia, etc. If you want multiple dispatch in C++ and its kin, you need to jump through hoops with visitor patterns and such.
Re:What can we learn from how compilers are designed? by foota on Friday March 16, 2018 @08:39PM (#16604992)
The difference confused me for a bit, but consider the following:

  Print(Vehicle v){
    print("vehicle");
  }

  Print(Car c){
    print("car");
  }

  Print(Boat b){
    print("boat");
  }

  Vehicle test = Boat();
  
  Print(test);

In a language with multi dispatch I believe this prints boat, whereas with single dispatch it prints vehicle.
Re:What can we learn from how compilers are designed? by lmitchell on Friday March 16, 2018 @10:33PM (#16605319)
Not quite. Multiple dispatch (as mentioned in sibling comments) is basically 'virtual on both object and argument types', so like:

  class Vehicle {
    virtual void Collide(Vehicle other);
  }

  class Car {
    void Collide(Truck other) { /* hit a truck */ }
    void Collide(Car other) { /* hit another car */ }
  }

  class Truck {
    void Collide(Truck other) { /* hit another truck */ }
    void Collide(Car other) { /* hit a car */ }
  }

  Vehicle c = Car();
  Vehicle t = Truck();

  c.Collide(t); // with multiple dispatch, calls Car::Collide(Truck)
Re:What can we learn from how compilers are designed? by seanmcdirmid on Friday March 16, 2018 @11:36PM (#16605485)
You are using static overloading on the argument rather than dynamic dispatch. If the static type of truck is obscured to a super class, your behavior won't be as expected. In general, overloading shouldn't be used as a replacement for dynamic dispatch, rather it should be used to accommodate multiple incompatible types (e.g. foo(X) and foo(Y) but never foo(Z) if X <: Z and Y <: Z unless foo(Z) maintains the behavior of foo(X) and foo(Y) given an x or a y under subsumption).

Multiple dispatch is typically meant to be purely dynamic in meaning, as wiki puts it:

> Multiple dispatch or multimethods is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case some other attribute, of more than one of its arguments.[1]

(https://en.wikipedia.org/wiki/Multiple_dispatch)

Re:What can we learn from how compilers are designed? by yorwba on Saturday March 17, 2018 @01:28AM (#16605720)
The static type of both c and t is Vehicle, but by dynamic multiple dispatch Car::Collide(Truck) is selected. That's not how C++ works, but the example was exactly about illustrating how C++ doesn't work.
Re:What can we learn from how compilers are designed? by barrkel on Friday March 16, 2018 @09:16PM (#16605097)
Multiple dispatch is overriding virtual member functions on argument types, not just receiver type.

Overloading and overriding are duals; the former is statically determined, the latter dynamically determined. Just like you can overload on the full argument list, multiple dispatch lets you override on the full argument list. This lets you do things like implement the visitor pattern without double dispatch. And C++ doesn't support it without libraries.

Re:What can we learn from how compilers are designed? by zenhack on Friday March 16, 2018 @08:42PM (#16605000)
Unless it's via some new-fangled C++ feature I haven't heard of yet, I'm pretty sure C++ doesn't have multiple dispatch. Can you elaborate on what you're referring to?
Re:What can we learn from how compilers are designed? by bstamour on Saturday March 17, 2018 @08:20AM (#16606643)
C++ doesn't have multiple dispatch. I think the OP was referring to multiple inheritance, which is not the same thing.
Re:What can we learn from how compilers are designed? by ZephyrP on Saturday March 17, 2018 @04:18PM (#16608857)
I was thinking of runtime single dispatch with compile-time type of the arguments to that function.
Re:What can we learn from how compilers are designed? by bstamour on Saturday March 17, 2018 @05:01PM (#16609043)
So virtual function overloading?

Can you give a small bit of sample code to demonstrate what you mean?

Re:What can we learn from how compilers are designed? by ZephyrP on Saturday March 17, 2018 @08:17PM (#16609835)
just ordinary virtual function that discriminates upon arguments (at compile-time).
Re:What can we learn from how compilers are designed? by zenhack on Saturday March 17, 2018 @10:42PM (#16610245)
Yeah, multiple dispatch implies more than one argument involved in the dispatch decision at runtime. So that's not the same thing.