-
Notifications
You must be signed in to change notification settings - Fork 37
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 bidnings to TextEncoder/TextDecoder #78
base: main
Are you sure you want to change the base?
Conversation
Looks like a good start! Since this is creating a new type rather than reusing something from ReScript, feel free to use the records-as-objects capability of ReScript instead of
Then your code can look like this: let e = TextEncoder.make()
e.encoding // "utf-8" |
Thanks, @TheSpyder TIL a binding pattern! |
Haha yeah it's not obvious but a really nice ergonomic way to do bindings. So, two more things before we can merge:
|
There is also Since the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for not picking up on the other thing that needs changing when I reviewed last week.
%%private( | ||
@new external _makeWithOptions: (string, decoderOptions) => t = "TextDecoder" | ||
|
||
@obj | ||
external makeDecoderOptions: ( | ||
~fatal: option<bool>=?, | ||
~ignoreBOM: option<bool>=?, | ||
unit, | ||
) => decoderOptions = "" | ||
) | ||
|
||
@new external make: unit => t = "TextDecoder" | ||
|
||
let makeWithOptions = (~encoding="utf-8", ~fatal=?, ~ignoreBOM=?, ()) => | ||
_makeWithOptions(encoding, makeDecoderOptions(~fatal, ~ignoreBOM, ())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I appreciate the sentiment to make the bindings more usable, we want these bindings to be zero-cost as much as possible. In this case ReScript 10 should have a new feature that makes makeDecoderOptions
unnecessary, but it will be easier to switch to the upcoming style if the exported API takes a decoderOptions
.
Please remove the private wrappers and export the more difficult to use way for now; here and below.
%%private( | |
@new external _makeWithOptions: (string, decoderOptions) => t = "TextDecoder" | |
@obj | |
external makeDecoderOptions: ( | |
~fatal: option<bool>=?, | |
~ignoreBOM: option<bool>=?, | |
unit, | |
) => decoderOptions = "" | |
) | |
@new external make: unit => t = "TextDecoder" | |
let makeWithOptions = (~encoding="utf-8", ~fatal=?, ~ignoreBOM=?, ()) => | |
_makeWithOptions(encoding, makeDecoderOptions(~fatal, ~ignoreBOM, ())) | |
@obj | |
external makeDecoderOptions: ( | |
~fatal: option<bool>=?, | |
~ignoreBOM: option<bool>=?, | |
unit, | |
) => decoderOptions = "" | |
@new external make: unit => t = "TextDecoder" | |
@new external makeWithOptions: (string, decoderOptions) => t = "TextDecoder" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could still have a name argument for the encoding helps people to know that that parameter is supposed to be:
@new external makeWithOptions: (~encoding: string, decoderOptions) => t = "TextDecoder"
fe0f2e9
to
7f2cf0a
Compare
Both are available on browsers, Node.js, and Deno.
In most cases, it is used without arguments
I added some additional bindings to support the full specification.