This blog post will deal with an experimental approach to higher order functions at compile time, at the type level. It’s motivated in part by my previous post on ternary
Nat, Counting to Infinity at Compile time.
You needn’t understand the details of
TNat (or its binary cousin
We saw in a previous blog post of mine an introduction to counting at compile time in scala, using the conventional
Nat is slow. Really, really slow, and there are hard limits on what we can make the scala compiler do, as it currently stands.
Counting at compile time is one of the world’s simple pleasures. We have taken the scala compiler and hacked something in to it to make it do things it really doesn’t look like it should be doing.
It provides a mapping between arbitrarily nested types, provided the compiler can calculate a path between them, all powered with gratitude by
Generic from shapeless.
A quick example:
Target here could be any combination…
The library alphabet-soup can be found here.
Alphabet-soup is a scala library to provide faster compiler-time type-mixing, to reduce boilerplate and the cognitive overhead of reading and writing canonical code.
If there’s only one way to write a piece of code, why do we have to write it? …
This blog post will assume terminology from the functional scala library Cats. It is easily applicable to Scalaz, too.
Libraries like Cats and Scalaz give you a verb ‘traverse’, for easier handling of combinations of wrapper types.
It allows you to turn something clunky like this:
In a previous blog post I introduced a way to get unboxed arbitrary arity union types in scala. We can write
foo allows an
Int or a
String to be passed to it, and nothing else.
But escaping the union context, ie getting the
Int or the
When writing our project, we wanted to pass a static sorting object all the way through our stack from the API (where it was parsed automatically) to Slick, and have the database results be sorted automatically without any extra input from the end-user (ie you, the feature-developer).
(In this post…
Most strings that you store in a traditional SQL database have a maximum length allowed. This can frequently cause problems, since this maximum length isn’t known by your code.
You might have some checks dotted around, maybe on your API or DB layer, that only sensible length strings get into…