Functors for dependency injection?

It’s not that I disagree with that is said, it’s just that all this information doesn’t seem to be directly relevant to what is being asked in this thread. Put it this way, if I knew Java and was curious about OCaml, I would be just as confused as to whether I can - or should, use functors for DI in OCaml.

Naively restated, the OP is curious about the consequences of porting standard Java code like this:

interface Service {
  Response run(Request request);
}

class App {
  Service service;
  App(Service service) {
    this.service = service;
  }
}

into OCaml written in this style:

module type Service = sig
  type t
  val run : t -> Request.t -> Response.t
end

module App (Service : Service) = struct
  ..
end

I’m telling him that while it’s possible, there are some serious practical disadvantages that explain why the majority of OCaml code isn’t written this way. Discussions about immutability vs mutability, frameworks, whether DI is good or bad, the virtue of ML modules, do not help the OP to understand the trade-offs of this technique.

I probably came off a little too strong than I’ve liked in my first reply, but it just seemed like the OP asked a very concrete question, and received responses containing a lot of fascinating, but largely inapplicable theory crafting about OO and FP.

6 Likes