rescript-labs / decco

Bucklescript PPX which generates JSON (de)serializers for user-defined types
MIT License
226 stars 27 forks source link

Error when trying to use decco for variants #28

Closed mrmurphy closed 4 years ago

mrmurphy commented 5 years ago

Very strange, but when I drop this into any file in my project:

[@decco]
type bobby =
  | A
  | B
  | C;

I get the following error message:

  We've found a bug for you!
  (No file name)

  The constructor Js.Json.JSONString belongs to the variant type
    Js.Json.tagged_t = Js_json.tagged_t
  but a constructor was expected belonging to the variant type option

Decco version 0.1.0, bs-platform version 5.2.1.

Thank you!

baransu commented 4 years ago

Experiencing the same issue 🙁

ryb73 commented 4 years ago

I'm not able to reproduce this. Can you provide your bsconfig?

baransu commented 4 years ago

Repro: https://github.com/baransu/ppx_decco-variant-error-repo Code:

[@decco]
type status =
  | Active
  | Inactive(float);

[@decco]
type user = {
  name: string,
  status,
};
ryb73 commented 4 years ago

Thanks for the repo. It looks like it's caused by opening Belt. I'll take a closer look later

ryb73 commented 4 years ago

TIL OCaml's array indexing syntax is just syntactic sugar for Array.get. In other words, var[0] is equivalent to Array.get(var, 0)

This is the source of your error: OCaml's native Array.get returns the actual value directly (throwing an error if the index is out of bounds), while Belt.Array.get returns an option. When you open Belt, it changes the behavior of var[0].

See: https://stackoverflow.com/a/46394530/1953740

I guess I can fix it by avoiding that array syntax and just using Belt.Array directly.

ryb73 commented 4 years ago

Deployed a fix in version v0.2.2 for BS 6 and v0.1.1 for BS 5.

baransu commented 4 years ago

Thank you a lot!