4 ms·
This is exactly what higher-order functions do, without the ad-hoc OO junk that's slightly different in every language.
by l_dopa 9y ago
This is exactly what higher-order functions do, without the ad-hoc OO junk that's slightly different in every language.
- nerdponx 9y agoI'm not talking about higher order functions. I'm just talking about regular old dispatch, like in C++.
- yawaramin 9y agoIn a way C++ is using higher-order functions conceptually too for polymorphic runtime dispatch. Because it uses vtables, which are lists of function pointers--essentially the object doing the dispatch is a HOF which calls one of the functions it has a pointer to. All this is just made explicit, and the boilerplate OOP mechanisms stripped away, when you just directly pass first-class functions around.
- nerdponx 9y agoI guess I just don't understand the connection between first class functions and multiple dispatch. I have a function "add()" that works differently for strings and numbers. How can I replicate that behavior with higher order functions?
- yawaramin 9y agoHere's an example (top-left corner, ReasonML syntax): https://reasonml.github.io/try/?reason=LYewJgrgNgpgBAQTGOBeOBvAUHOsAucAlgHb4AUAHgDRwCeAlGnJXANT0DcOeMhAzvgBOpAOZVajZqzYc63XATggADuQCGyGENo16TdJrDaJ+7gF9uWJUJj9o+AIzMkYAHSryrt6Xy1HtABMDNw2dg6BLsgeat6CIiSitABEABYwUFAgKSkA7iBCUGDJIVhAA https://reasonml.github.io/try/?reason=LYewJgrgNgpgBAQTGOBeO... The key idea is delegating the type-specific operations to specific implementations which handle them, and then statically (at compile time) choosing which specific implementation you're calling. In OCaml (ReasonML) you have to pass in the implementations manually, but Haskell and Scala have the ability to automatically choose the implementation based on the types of the arguments, so that it looks dynamic even though it's static and type-safe.