-
Notifications
You must be signed in to change notification settings - Fork 450
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
Revisit the design of bindings. #6211
Comments
An example from the field: https://github.com/cca-io/rescript-material-ui/blob/mui-v5/packages/rescript-mui-material/src/components/Accordion.res This illustrates the idea that one can often create natural bindings with zero magic -- just use the language. CC @fhammerschmidt who provided this |
Played around with What if Inline ffi to use es6 syntax from userlandmodule Array = {
let concat: (
array<'value>,
array<'value>,
) => array<'value> = %ffi(`(arr1, arr2) => [...arr1, ...arr2]`)
}
let array = Array.concat([1, 2], [3, 4]) This currently generates: var concat = ((arr1, arr2) => [...arr1, ...arr2]);
var $$Array = {
concat: concat
};
var array$1 = concat([
1,
2
], [
3,
4
]); But what if the function body of var array$1 = [...[1, 2], ...[3, 4]] Notice it's using es6 spread syntax, but fully controlled via the definition of Essentially how a regular function works today, when ReScript sees it can be inlined, but exclusively for Inlining to produce clean JSAnother example: type element
type window
@val external window: window = "window"
let getActiveElement: window => option<element> = %ffi(`w => w.activeElement`)
let activeElement = window->getActiveElement This now generates: var getActiveElement = (w => w.activeElement);
var activeElement = getActiveElement(window); But if var activeElement = window.activeElement; ...which would be idiomatic JS. No idea how far one could take this or if it even makes sense, but it does open up some interesting ideas. Inlining to do
|
Technically just 2 things to be careful with:
|
Looking at it again, one of my examples have inlining inside of the ffi itself. Array.concat inlines both of the arrays into the spread, which is inside the ffi. I guess that's what we'd need to opt out from, inlining inside of the ffi itself. Injecting JS produced by the compiler into the ffi code itself seems unlikely to be viable (although it'd be fantastically cool). Maybe this is what you meant with your response too. |
The inlining from your example suffers from these potential issues:
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Bindings are one of the most complicated parts of the language, which require understanding a dedicated domain specific language of annotations.
There is also the question of how to generate bindings automatically. This in turn raises the question of expressivity, as not all the TypeScript type language maps nicely to ReScript types.
Finally there's the question of whether one should "trust" the types declared in bindings, or check them. (genType has been experimenting with generating some code to be checked by TS in order to verify consistency).
One of the recent trends is to only bind the parts necessary for a specific project, rather than writing complete bindings for a library.
There's also the frequent suggestion from community members to take existing bindings for a project, and adapt them to your needs when required, rather than trying to have a big blessed repository of bindings.
In the spirit of recent trends, the goal of this issue is to explore just "using the language" for writing bindings, instead of relying on a custom domain specific language of annotations.
The result would be a bit more verbose, but potentially could lower the learning curve for writing bindings.
In particular, the idea is that a user not familiar with ReScript should be able to read existing bindings and immediately understand what they mean, and modify them.
One special such user is AI, so that one goal is to make it as easy as possible for bindings to be generated automatically.
Here are some specific thoughts about one possible step in that direction: https://gist.github.com/cristianoc/00e760e1d5605ddc36fba29d1b1f14c3
Some related issues:
%raw
#6213%raw
#6214%ffi
and local type definitions on a number of bindings and report on the experienceThe text was updated successfully, but these errors were encountered: