3 ms·
What would multi-dispatch do differently???
by brabel 5y ago
What would multi-dispatch do differently???
- adgjlsfhk1 5y agothe difference is that overloading works on compile time types but dispatch works on runtime types. This matters because when you write a generic algorithm, you don't know the type, so don't get the right behavior with overloading (you get the generic fallback).
- brabel 5y agoIn Groovy, dynamic dispatch is used. It's the runtime types that matter, as my example makes patently clear. EDIT: to make it even more obvious there's no difference: class Fire{} class Water {} class Grass {} class Ice {} def eff(Fire f, Grass g) { 'burn' } def eff(Fire f, Ice ice) { 'melt' } def eff(Water w, Grass g) { 'grow' } def eff(Water w, Ice ice) { 'freeze' } def eff(a, b) { 'undefined' } // hide compile-time types Object fire = new Fire() Object grass = new Grass() Object ice = new Ice() Object water = new Water() println eff(fire, grass) println eff(fire, ice) println eff(water, grass) println eff(water, ice) println eff(ice, water) Prints: burn melt grow freeze undefined