Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

of_yojson_exn breaks code that defines of/to_yojson by hand #76

Closed
emillon opened this issue Apr 19, 2018 · 3 comments · Fixed by #86
Closed

of_yojson_exn breaks code that defines of/to_yojson by hand #76

emillon opened this issue Apr 19, 2018 · 3 comments · Fixed by #86

Comments

@emillon
Copy link
Contributor

emillon commented Apr 19, 2018

I'm commenting a bit late on the issue, but adding of_yojson_exn (see #68) breaks all code that defines of_yojson by hand ([@@deriving of_yojson] in the mli, let of_yojson in the ml).

This means that to support this, users will have to copy/paste let of_yojson_exn json = match of_yojson json with Ok x -> x | Error e -> failwith e or similar. It would be nice if ppx_deriving_yojson could handle these cases automatically, but it's unfortunately not possible.

I suggest that we revert this part before the next release and advise users to use a combinator like CCResult.get_exn or RResult.R.get_ok. An alternative is to make this feature opt-in: for example if [@@deriving yojson { exn = true }] is present, generate the of_yojson_exn function in the .ml, and add the corresponding val in the .mli.

Thanks!

@whitequark
Copy link
Collaborator

cc @stevebleazard

I think the right path here is to make the feature opt-in, not revert. Opinions @ocaml-ppx/deriving?

@emillon
Copy link
Contributor Author

emillon commented Apr 20, 2018

To make this feature play nice with existing code, we could also always use of_yojson from dependencies instead of calling of_yojson_exn recursively. That way, it can use dependencies that do not have exn = true (that's similar to how show uses pp, so defining pp in libraries is enough).

@whitequark
Copy link
Collaborator

I agree that this is better.

gasche added a commit to gasche/ppx_deriving_yojson that referenced this issue Dec 15, 2018
fixes ocaml-ppx#76

Adding new functions to the default generated interface
(and signature) is problematic for user code that uses [@@deriving
yojson] in signatures, but manually implements the two
`{of,to}_yojson` functions (and nothing else) in their
implementations. There is plenty of code in this style in OPAM.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants