0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Advent of JustinAdvent Calendar 2018

Day 13

AOJ day 13: Type-level annotated string templates

Last updated at Posted at 2018-12-12

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.

https://qiita.com/kimagure/items/6729a5d55ab99bcee8ec

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:

https://qiita.com/kimagure/items/5c3f3fcb898e480c56be

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?