-
Notifications
You must be signed in to change notification settings - Fork 453
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
RFC: @deriving
for labeled arguments
#6530
Comments
@DZakh noticed another pattern related to this The props mapping is sometimes used even within higher-order functions. But this proposal does not handle the case, for example: let fn = (~a, ~b) => {
(~c, ~d) => {
a + b + c + d
}
}
Updated the RFC to handle this case |
It can be done by PPX implementor, see #6537 |
Implementation is currently blocked by #6539 |
@deriving
for labeled arguments@deriving
for labeled arguments
@deriving
for labeled arguments@deriving
for labeled arguments
Motivation
JavaScript doesn't support labeled arguments, but ReScript does. So
let add: (~a, ~b)
in ReScript compiled tofunction add(a, b)
, so the labelsa
andb
cannot be referenced by the call site.In JS/TS codebase it is common to use object destructuring for this (e.g. props in React components) Also called RORO pattern
Until v10, gentype created a runtime mapper for this. However, since v11 it no longer generates runtime mappers. In the long term, gentype shouldn't have any runtime (See #6196)
However, the runtime mapper for props is an important feature for interoperability not only with TypeScript but also with JavaScript codebases in general. There must be an alternative to completely removing the feature.
This proposal solves the problem by extending the
@deriving
tag existing in the ReScript core.Detailed Design
Basic
produces
This is only valid for functions with labeled arguments, and requires that the
@genType
tag be duplicated if it exists.High-order functions
@deriving(params)
can only be used on top-level functions. This is because new type bindings cannot be defined in a closure. Btw, user may want mappers for nested functions like:If the return type of the function annotated by
@deriving(params)
is a function, then it should producesDeriving is applied in nested functions, but ends when it encounters a function with no labeled parameters.
Customizing definitions
User can customize derived typenames to make it more useful in TypeScript side.
Implementation
TBD
The text was updated successfully, but these errors were encountered: