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 8

AOJ day 8: Type-level path parameters

Last updated at Posted at 2018-12-07

Typically, approaches to represent parameterized paths in the type level have required making some kind of type-level operator of kind Type -> Type -> Type, so that you could define routes in a manner such as type MyRoute = Literal "hello" / Param "name". While this seems "explicit", it also seems at least a little bit silly: why can't we use Symbols to define these routes?

Csongor wrote about how Symbol.Cons was implemented for PureScript, with an example library for deriving variadic functions here: http://kcsongor.github.io/purescript-safe-printf/. He also then made a record-based formatting library here: https://github.com/kcsongor/purescript-record-format. Surely we can take advantage of these to make a type-level path solution?

"Well-typed path params in PureScript 0.12"

In this post, I talked about how we can use the type-level parsing result from record-format to make a path:

https://qiita.com/kimagure/items/3273d20c4c5ad74dbe26

Knowing that we are going to parse URLs, we can use / as delimiters, where we can match literal and variable sections. In building up the variable results, we can use Record.Builder to build up a record of the parameters we have parsed into a record of strings, where the key is the name that we gave our parameter in this scheme:

url :: SProxy "/hello/{name}/{age}"

In the future, we'll talk about how we can add optional type annotations to this Symbol, so that we don't need to work with a record of strings being converted by key to a proper heterogeneous record.

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?