janestreet / ppx_yojson_conv

[@@deriving] plugin to generate Yojson conversion functions
MIT License
59 stars 8 forks source link

Return type of decoders #8

Open psteckler opened 3 years ago

psteckler commented 3 years ago

For a type t, the ppx generates a value t_of_yojson : Yojson.Safe.t -> t. Hence, if the JSON doesn't correspond to a t instance, the decoder raises an exception (or has some undefined behavior).

In ppx_deriving_yojson, the generated decoders produced a Result.t, making it easy to know when there's an error.

What is the reason to favor raising an exception, rather than revealing errors in the returned value?

emekoi commented 3 years ago

would it be possible to add a want_result option like the want_exn option in ppx_deriving_yojson to generate decoders that return a result?

zoj613 commented 4 months ago

Would be nice if we could have an option to return a Result.t indeed.