Skip to content

Instantly share code, notes, and snippets.

@cvogt
Created October 22, 2014 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvogt/c1303f0dfaa2a87f6377 to your computer and use it in GitHub Desktop.
Save cvogt/c1303f0dfaa2a87f6377 to your computer and use it in GitHub Desktop.
Type-indexed maps for fun and functional dependency injection
Type-indexed maps (TMap) are data structures that support type-safe,
constant time lookups of elements by their type. They are unordered,
allow structural typing and can be used as composable records. Unlike
HList-based implementations as in Shapeless, TMaps are implemented
using subtyping, which leads to pretty readable types and error messages.
TMap types can easily be written by hand.
TMaps are a perfect fit for monadic dependency injection. They enable
the Reader monad to be used for multiple dependencies with zero boiler
plate. The combination can be viewed as a partial implementation of
implicit parameters as a library rather than a compiler feature. Unlike
Scala's native implicit parameters, this allows complete type-inference
without having to specify them in every method signature.
TMaps can also be used to emulate named arguments, tuples and structural typing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment