Closed c-cube closed 5 years ago
maybe it's more like having tuple0, tuple1, tuple2, tuple3, tuple4
for matching lists of exact size with heterogeneous elements, and tuple4_tl : (value * value * value * value * value list)
to get the rest of the list (so you can have 10 elements if you chain tuple4_tl twice and then tuple2).
So in #7 I ran into the same problem (decoding dune
sexps) and went with this:
That seems pretty neat, indeed. Perhaps we could also have exact matchers (i.e. val exact_string : string -> unit decoder
and exact_int : int -> unit decoder
) that allow you to write:
type t = Foo of bool * int list | Bar of int
let decode_t =
one_of [
"foo", (exact_string "foo" >>=:: fun () ->
bool >>=:: fun x -> list int >>= fun y -> success (Foo(x,y)));
"bar", (exact_string "bar" >>=:: fun () -> int >>= fun x -> success (Bar x));
]
Yes that sounds useful. I wonder if an API that re-uses the existing combinators would work, something like:
val literal : eq:('a -> 'a -> bool) -> 'a decoder -> 'a -> unit decoder
let foo_decoder = literal string "foo" ~eq:String.eq
I'd make eq
an optional parameter, I think, but this looks quite nice indeed.
Another point is that instead of one_of
one could dispatch the decoder based on the first literal (first, uncons
, then decode a string, and then matching over that.) But it can be done in pure OCaml I think:
uncons string >>= function
| "foo", tl -> …
| "bar", tl -> …
| s -> failf "unknown constructor %s" s
Yep. A lot of my decoders look like this:
field "tag" string >>= function
| "foo" -> decode_foo >|= fun x -> Foo x
| "bar" -> decode_bar >|= fun y -> Bar y
| _ -> fail "Expected a Foo or a Bar"
Note that you don't need to capture the s
in the fail case - decoders
will capture the full context and display it in the error message for you.
when encoding sum types into json, sometimes one may do
["foo", 1, ["hello", "world"]]
to represent `Foo (1, ["hello"; "world"])). I'm not sure how this is handled by the current decoder API (it's more a tuple than a list).So would such an operator be relevant?
(other names might be better)
usage along the following lines: