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

Cannot use non-int default value with to_yojson #79

Open
alexozer opened this Issue Oct 4, 2018 · 1 comment

Comments

Projects
None yet
3 participants
@alexozer

alexozer commented Oct 4, 2018

Applying default value to record fields that aren't of type int seem to fail with a type error when using [@@deriving to_yojson]:

This seems to work:

type thing = {
  n : int [@default 0];
  str : string [@default "sdfasdf"];
  lst : int list;
}
[@@deriving of_yojson]

But when using to_yojson or yojson instead of of_yojson:

type thing = {
  n : int [@default 0];
  str : string [@default "sdfasdf"];
  lst : int list;
}
[@@deriving to_yojson]

I get:

Error: This expression has type string but an expression was expected of type
         int

Eliminating the default on string works with to_yojson and yojson, however:

type thing = {
  n : int [@default 0];
  str : string;
  lst : int list;
}
[@@deriving to_yojson]

@whitequark whitequark added the bug label Oct 4, 2018

@gasche

This comment has been minimized.

Show comment
Hide comment
@gasche

gasche Oct 5, 2018

Contributor

Which version of OCaml and ppx_deriving_yojson are you using? I cannot reproduce the problem under 4.06.1 or 4.07.0. The generated code is as follows (ocamlfind ocamlc -package ppx_deriving_yojson -dsource -c test.ml):

type thing =
  {
  n: int [@default 0];
  str: string [@default "sdfasdf"];
  lst: int list }[@@deriving to_yojson]
let rec (thing_to_yojson : thing -> Yojson.Safe.json) =
  ((let open! Ppx_deriving_yojson_runtime in
      fun x ->
        let fields = [] in
        let fields =
          ("lst",
            ((fun x ->
                `List
                  (List.map (fun (x : Ppx_deriving_runtime.int) -> `Int x) x))
               x.lst))
          :: fields in
        let fields =
          if x.str = "sdfasdf"
          then fields
          else
            ("str",
              (((fun (x : Ppx_deriving_runtime.string) -> `String x)) x.str))
            :: fields in
        let fields =
          if x.n = 0
          then fields
          else ("n", (((fun (x : Ppx_deriving_runtime.int) -> `Int x)) x.n))
            :: fields in
        `Assoc fields)
  [@ocaml.warning "-A"])

In particular, "sdfasdf" is correctly tested against the x.str field, which has type string as expected.

Contributor

gasche commented Oct 5, 2018

Which version of OCaml and ppx_deriving_yojson are you using? I cannot reproduce the problem under 4.06.1 or 4.07.0. The generated code is as follows (ocamlfind ocamlc -package ppx_deriving_yojson -dsource -c test.ml):

type thing =
  {
  n: int [@default 0];
  str: string [@default "sdfasdf"];
  lst: int list }[@@deriving to_yojson]
let rec (thing_to_yojson : thing -> Yojson.Safe.json) =
  ((let open! Ppx_deriving_yojson_runtime in
      fun x ->
        let fields = [] in
        let fields =
          ("lst",
            ((fun x ->
                `List
                  (List.map (fun (x : Ppx_deriving_runtime.int) -> `Int x) x))
               x.lst))
          :: fields in
        let fields =
          if x.str = "sdfasdf"
          then fields
          else
            ("str",
              (((fun (x : Ppx_deriving_runtime.string) -> `String x)) x.str))
            :: fields in
        let fields =
          if x.n = 0
          then fields
          else ("n", (((fun (x : Ppx_deriving_runtime.int) -> `Int x)) x.n))
            :: fields in
        `Assoc fields)
  [@ocaml.warning "-A"])

In particular, "sdfasdf" is correctly tested against the x.str field, which has type string as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment