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

Flattening of option values #59

Open
xclerc opened this issue Sep 1, 2017 · 2 comments
Open

Flattening of option values #59

xclerc opened this issue Sep 1, 2017 · 2 comments
Labels

Comments

@xclerc
Copy link

xclerc commented Sep 1, 2017

I am unsure how important this is in practice, but the encoding of
option values makes Some (Some (... None)) equal to None.
Could this be an issue [when using types whose definitions are unknown]?

module M : sig
  type t[@@deriving show, yojson]
  val x : t
  val y : t
end = struct
  type t = int option [@@deriving show, yojson]
  let x = None
  let y = Some 1
end

type t = {
  n : string;
  v : M.t option;
}[@@deriving show, yojson]

let x = { n = "x"; v = Some M.x; }

let y = { n = "y"; v = Some M.y; }

let roundtrip v =
  match of_yojson (to_yojson v) with
  | Ok v' ->
    if v = v' then
      Printf.printf "equal\n%!"
    else
      Printf.printf "%S vs %S\n%!" (show v) (show v')
  | Error _ -> assert false

let () =
  roundtrip x;
  roundtrip y
@xclerc xclerc added the question label Sep 1, 2017
@emillon
Copy link
Contributor

emillon commented Sep 7, 2017

That's the case with any type whose serialization can include null. For example [%of_yojson: unit option] serializes both Some () and None as null. I don't think it's a problem (and it's not really fixable), but that's probably worth mentioning in the "semantics" paragraph.

@xclerc
Copy link
Author

xclerc commented Sep 15, 2017

Thanks for the feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants