What Is Type-Level Programming?
This is going to be a long comment but I think it explains the post a little better than the post itself did, for whoever is interested in fully grokking this.<p>This style of programming is used a lot in functional programming. The analogy I always have in my head is designing furniture that customers build at home. That is because you can&#x27;t rely on customers having read the manual as an excuse when things go badly, and you have to assume that anything is possible for them to try will at some point will be attempted. In the furniture world a solution to this would be making everything fit together exactly one way, and only one way, so even if customers just try ever permutation they will eventually stumble upon the correct order of events.<p>In programming that could be generalized further to be something like &#x27;the &quot;next_step&quot; always requires some output from the &quot;current_step&quot;&#x27;, and many of us are already accustomed to seeing this in REST, as a lot of REST API&#x27;s will require some kind of token or id or basically reference to a previous operation to continue with future operations. The downside is that you can still provide invalid inputs, and the program continues just fine (e.g. you can make up a reference to a previous operation using any string or whatever other data type is required). (Like, how many of us prototype code using REST API&#x27;s with a little throwaway script that we just keep running, each step along the way making incremental progress, only to wrap the whole thing in a function when we&#x27;re done?)<p>The &quot;type-level&quot; programming (I&#x27;ve actually seen this go by a few names) improves upon this REST analogy by making it even more like our furniture analogy, by not even permitting you to &quot;try&quot; the next step unless you have a type that one can only have obtained from the current step. This is like the &quot;furniture only goes together one way&quot; example, but more flexible because it allows the furniture to go in arbitrarily many ways so long as they are legal. In fn programming this is done pretty easily by just having simple types that wrap a value, but restricting where those types can be created to only be allowed within the library. (And to me these kinds of APIs actually work better than the REST ones because I can pretty much know that if I have the right types, everything is going to work. So my development stays in compile-time land, and not incremental test-and-set runtime land a là REST and references)<p>One thing the author didn&#x27;t mention is what the downsides are of this kind of technique. From my experience there are really only two:<p>One minor one is API discoverability, since the types can become numerous, and from a user perspective it becomes difficult to read the API, since you have to keep tracing back. It&#x27;s hard from a user&#x27;s perspective to know where the &quot;entry&quot; point of the API is, and often times users don&#x27;t really spend enough time reading code and prefer to find examples. However, this isn&#x27;t really the end of the world because it&#x27;s like furniture and things only go together the &quot;right&quot; way. Usually a few examples is enough documentation to give developers an idea of the spirit of this API, just like how people making the furniture might only look at the picture on the box.<p>To me a larger issue is it can present a serious challenge to API designers. The APIs become very brittle and changes can break compatibility. This is often resolved by adding new versions of the API while deprecating the old ones, but this then makes things exponentially (literally) more difficult for the documentation and discoverability issue that I mentioned above, especially if there are a lot of examples online. It&#x27;s not like you can recall examples that other people have written, even if that&#x27;s technically what versions do.<p>So in the end, this is a great approach and one the things that fn programmers sort of eventually do intuitively, where we view the whole program as just a bunch of inputs and outputs that, when designed well, only fit together &quot;the right way&quot;, but it&#x27;s difficult and you&#x27;re ultimately just moving complexity around. In this case, I think a case could be made for you moving the complexities to &quot;good places&quot; by making the compromise of &quot;NO BAD&quot; in exchange for &quot;maybe it&#x27;s harder for the API designer to maintain and the user to discover&quot;.<p>I guess it slows down compilation too.<p>Personally, I&#x27;m not aware of better alternatives to this kind of pattern. At least not for similar kinds of problems, although maybe other people have their own ways they&#x27;ve seen similar &quot;furniture&quot; problems solved in other, novel ways. Ultimately it comes down to who is using this and for what. It&#x27;s appropriate for something that get&#x27;s shipped to other developers, but I&#x27;ve also seen people go way too far and design test code that works like this and I&#x27;m like &quot;...&quot;. So I want to put the don&#x27;t take it as gospel disclaimers out there.