-
Notifications
You must be signed in to change notification settings - Fork 455
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
Add support for @optional
in record declarations and remove @obj
.
#5423
Conversation
e81d2b3
to
c531f6e
Compare
@optional
in record declarations.
I love this new syntax. Super awesome!! 👍 |
There's still the limitation that it's not possible to take an existing record and set an optional field to None. |
This |
This is ready to try now: #5423 Notice there's direct support for |
Got it. I'll work on it! |
@optional
in record declarations.@optional
in record declarations and remove @obj
.
Simple change that only affects {x:e} when x is of option type. Now `e` is wrapped in an option automatically. Loses: the possibility to do `{x:None}`.
Still does not have a dedicated error message. Still requires to write the option type in full.
Still errors todo.
36d6cd9
to
5bd5761
Compare
Love the proposal, but I think it's simpler in surface area (and I think easier in usage) if for the optional values always an optional value is expected. So setting let v = { x: 3, y: Some(4) } And if let v = { x: 3, y: y } Now people only need to learn the optional on the type definition site. And refactors are also easier, because if an optional value is part of a record but not yet |
Unfortunately that's not expressive enough to write JSX. |
Hmm, I guess you could still have a field being
This way, if you really don't want to touch the existing code base, you could just do this and omit it in the places where you don't want it at all. |
In general, if a field becomes optional, then there's no way to get field update and access work without changes, whatever the proposal. |
The current proposal lets you keep all the updates unchanged, and requires changing lookups (as x.foo is now optional). |
Ah I understand that this is also determining if a key appears in the object. Got it! In that case I think the current implementation makes sense, but having a syntax sugar would definitely be nice! |
The task of adding surface syntax is up for anyone interested in having a go. |
Cool. What happens when ALL fields are optional? I don't know if it's possible type system wise, but it would be really nice if you can pass |
Created an issue rescript-lang/syntax#544 |
It is possible for them to be all nullable. But there's currently no syntax to populate that value. There's only a dirty trick one can use: type t = { @nullable x: int }
let empty = { x: @nullable None } which anyway is not literally the empty object. |
To have an empty object, one needs some syntax, and a check that the compiler internally does not scream with some assertion failure or something. |
Add support for
@optional
in record declarations. This supersedes@obj
, which is now removed.This defines a record with mandatory field
x
and optional fieldy
:A value can be constructed as
to leave field
y
unspecified.Or it can be constructed as
to specify the value of
y
.In each case,
y
is of optional type.The remaining thing to support is setting an optional field with an optional value. This is done by annotating the expression with
@optional
:This can also be used to assign any optional values:
This can be used to directly represent passing optional props to a component:
In case of inconsistency between implementation and interface, an error message outlines where is the difference in
@optional
annotations.