Created
October 22, 2014 10:59
-
-
Save cvogt/c1303f0dfaa2a87f6377 to your computer and use it in GitHub Desktop.
Type-indexed maps for fun and functional dependency injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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