Get the latest tech news

Basic dependency injection in OCaml with objects


A simple way to encode dependency injection using the Reader monad and objects in OCaml (to work well with type inference).

In his article Why I chose OCaml as my primary language, my friend Xavier Van de Woestyne presents, in the section , two approaches to implementing dependency injection: one using and one using modules as first-class values. For my part, I find that dependency injection makes unit testing a program trivial, which I think is reason enough to care about it (For example, the time-tracker I use at work, Kohai, uses Emacs as its interface and the file system as its database. In OCaml, the richness of the object model (and its structural capabilities) is a real advantage for this kind of encoding, allowing a drastic reduction of the boilerplate needed to manage multiple sets of dependencies.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of objects

objects

Photo of ocaml

ocaml

Related news:

News photo

Data, objects, and how we're railroaded into poor design (2018)

News photo

OCaml as my primary language

News photo

Here's the absolutely massive Tamagotchi Paradise next to other objects in the wild, for scale