Skip to content

Plans for optional records support #1

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

Closed
youngkidwarrior opened this issue Sep 20, 2022 · 5 comments
Closed

Plans for optional records support #1

youngkidwarrior opened this issue Sep 20, 2022 · 5 comments

Comments

@youngkidwarrior
Copy link

Rescript v10 has implemented support for optional record fields
rescript-lang/rescript#5654

Any plans on implementing this instead of using Option?

Is it even feasible with the API. I might take a look at it myself if you think it is possible

@glennsl
Copy link
Owner

glennsl commented Sep 23, 2022

Hi. I'm not aware of any syntax for conditionally setting an @optional field individually (the issue you reference seems to just be about a printing bug), but if there is I don't see why it wouldn't work with the existing API. Whether it uses options directly, or branching on an option. I don't know how else it would work.

@glennsl
Copy link
Owner

glennsl commented Oct 2, 2022

I'm assuming this has been answered adequately. But feel free to re-open if necessary.

@glennsl glennsl closed this as completed Oct 2, 2022
@hellos3b
Copy link

hellos3b commented Jan 3, 2023

I'm facing this as well, when using the optional ? syntax, the field.optional doesn't resolve right

Screenshot 2023-01-03 at 9 44 49 AM

type foo = {
  a: string,
  b?: string,
}

let decoder = {
  open Json.Decode
  object(field => {
    a: field.required(. "a", string),
    b: field.optional(. "b", string),
  })
}

I think it would make more sense if field.optional<'a> returns 'a instead of option<'a>, and then use the Decode.option(string) for the explicit option type

let decoder = object(field => {
  a: field.required(. "a", option(string)),
  b: field.optional(. "b", string)
}

@glennsl
Copy link
Owner

glennsl commented Jan 8, 2023

I'm facing this as well, when using the optional ? syntax, the field.optional doesn't resolve right

According to the documentation, if you want to assign an option to an optional field, you need to prefix it with ?:

type foo = {
  a: string,
  b?: string,
}

let decoder = {
  open Json.Decode
  object(field => {
    a: field.required(. "a", string),
    b: ?field.optional(. "b", string),
  })
}

I think it would make more sense if field.optional<'a> returns 'a instead of option<'a>, and then use the Decode.option(string) for the explicit option type

Then it wouldn't be optional... You can get this behavior by using field.required instead, but the implication of using ´option´ is that you're not able to distinguish between the field not being present and the the type being wrong. Edit: Strike that, this is how it used to work with bs-json, which was closer to Elm's decoders. option now only returns None if it's null, not if the inner decoder fails, and not if it's undefined either, since undefined is not a JSON value

@hellos3b
Copy link

hellos3b commented Jan 8, 2023

b: ?field.optional(. "b", string),

Ah man thanks, this is the syntax I didn't know existed! My suggestion was based off trying to get the types to agree on my own fork, you can disregard it :)

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

No branches or pull requests

3 participants