What the difference between Dependency Injection with higher order function and Functor

Read from the documentation that functor can be used to implement dependency injection pattern, now I’m wondering what is the difference between the regluar higher order function design? And when to use each one?

1 Like

Look at Map.Make: it creates a module where you can use each function without giving a compare function each time. (Then you are sure that the same function is always used!)

For simple tasks, like with List.sort, higher order function is fine.

1 Like

Functors used for dependency injection inject the dependencies statically at compile time. First class modules (or higher order functions) which are used for dependency injection, live in the value world and inject at runtime: it is sufficient at compile time if they satisfy their type.

1 Like

Not really an answer to your question, and sort of only tangentially related (F# isn’t OCaml after all), but maybe you might find this series to be interesting: Six approaches to dependency injection | F# for fun and profit. (You will see these are missing the functor, as F# doesn’t have them as OCaml does.)

Also, this video goes into some similar stuff: From Dependency injection to dependency rejection. (Again…in F#, but you may enjoy it as well.)

Anyway, these may give you some interesting ideas.

1 Like