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:
In a language with multi dispatch I believe this prints boat, whereas with single dispatch it prints vehicle.
[ Parent
| on HN ]
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]
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.