0
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

AOJ day 13: Type-level annotated string templates

Previously, I covered my post about well-typed paths using Symbols with formatting parameters, such that we can derive a parsing function from a Symbol proxy like so:

-- inferred type:
parseHelloURL :: String -> Either String { name :: String, age :: String }
parseHelloURL = parseURL (SProxy :: SProxy "/hello/{name}/{age}")

But really, we might like to read age here as an Int, and we could provide this information as a type annotation. And we can!

"Parsing type-level strings to extract types"

In this post, I go through how we can modify the previous implementation to conditionally parse out a type annotation from our Symbol, by using the same techniques for parsing piecewise with instance chain groups as done in Record-Format and other libraries.

This way, we can instead work with URLs that have their types annotated like so:

parseHelloURL :: String -> Either String { name :: String, age :: Int }
parseHelloURL = parseURL (SProxy :: SProxy "/hello/{name:String}/{age:Int}")

"Simple Routing based on parsing type-level strings"

There's also a follow-up post to this that applies classic RowToList techniques to allow for a row of these routes to be tried to product an Either of no matches and a Variant of the routes:

Register as a new user and use Qiita more conveniently

  1. You can follow users and tags
  2. you can stock useful information
  3. You can make editorial suggestions for articles
What you can do with signing up
0
Help us understand the problem. What are the problem?