package ppx_map
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=c0ac31ffa9cded558377eebf6a25037e
sha512=9dc615f364625857ffa58dead6e979dbc7db64711de21de767dedc0e52a06200c4f7ffa40fd4b84adfecb9a7d54bc05c8f4d5eba19e5a82bb3f1f71fab0da18d
Description
This package allows to declare maps in an expressive way using [key => value] expressions
Published: 24 Jun 2024
README
ppx_map
ppx_map is a PPX rewriter to simplify the definition of maps.
Usage
Simple cases
If the type of your map keys is simple enough (bool, char, float, int, string or unit) and the PPX can deduce it, it is as simple as:
[%map 0 => "zero"; 1 => "one"; 2 => "two"]which will give something similar to:
let module Int_map = Map.Make (Int) in
Int_map.(empty |> add 0 "zero" |> add 1 "one" |> add 2 "two")The extension is able to automatically type the map if the first key is a non-bound value (e.g. not defined by a let) of the types given above. For example,
let a = 0 in
[%map a => "zero"; 1 => "one"; 2 => "two"]will give the following compilation error:
Error: `map' cannot infer the type of this value. You need to give an explicit
bool, char, float, int, string or unit.whereas
let a = 0 in
[%map 1 => "one"; a => "zero"; 2 => "two"]will work just fine.
More complex cases
Empty maps
While it may seem trivial, creating an empty map requires a little more than just [%map]; a type must be specified:
[%map.Int]or
[%map Int]will do the trick. I don’t know which syntax I prefer; pick your own and stick to it!
Simple modules
If the first key you give is a bound value, you need to help the rewriter a little:
let (a, b, c) = (0, 1, 2) in
[%map.Int a => "zero"; b => "one"; c => "two"]or
let (a, b, c) = (0, 1, 2) in
[%map Int; a => "zero"; b => "one"; c => "two"]Again, you can decide which syntax you prefer.
Functors
You can also use functors! But only the second syntax is going to work:
[%map Functor (Module); key => value]These functors need to be of arity 1 (Functor (Module) (Module') cannot be used as it wouldn’t work well with OCaml’s parser). As we could expect, using a generative functor gives the following compilation error:
Error: This expression has type 'a $Map.t
but an expression was expected of type 'b
The type constructor $Map.t would escape its scopeDon’t do that!
Also, I don’t really see why you’d ever need to use functors here, but that was fun to implement 🙂