Skip to content
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

Emit tags as strings. #6088

Merged
merged 8 commits into from Mar 24, 2023
Merged

Emit tags as strings. #6088

merged 8 commits into from Mar 24, 2023

Conversation

cristianoc
Copy link
Collaborator

@cristianoc cristianoc commented Mar 20, 2023

Emit tags as strings.
Currently does not have support for @deriving(jsConverter) and the relevant tests have been removed.

Base automatically changed from pattern_match_variant to master March 21, 2023 10:34
Compile is_tag to `!== "object"` instead of `=== "string"`.
Also the comment is not emitted anymore, since there's always a tag.
Not special casing means that the representation is uniform, and does not change when the type is extended. This is important with zero cost ffi, where the runtime representation is exposed to the user, to reduce possible surprises.
@cristianoc cristianoc merged commit 86bf09c into master Mar 24, 2023
1 check passed
@cristianoc cristianoc deleted the emit_tags_as_strings branch March 24, 2023 15:31
@jacobp100
Copy link

Will there be a way to turn this off and go back to the old behaviour?

@cristianoc
Copy link
Collaborator Author

No

@jacobp100
Copy link

Some engines struggle with switching over a large amount of strings (hermes). Recently, React removed a few instances of these big string switch cases for that reason

It'd be a shame if there was no escape hatch when you know you have performance critical code

@cristianoc
Copy link
Collaborator Author

Please send your perf measurements.

@jacobp100
Copy link

I’ll write myself a note to do that after upgrading

@FreddieGilbraith
Copy link

@jacobp100, looks like you can use the @as tag as an escape hatch:

@tag("state")
type loadingState = | @as(1) Loading({ready: bool}) | @as(2) Error({message: string}) | @as(3) Done({data: bool})

let state = Error({message: "Something went wrong!"})

https://rescript-lang.org/try?version=v11.0.1&code=AIFwhg5gFARAzuEBTGBKAUCAngByQAgBsB7MAEwEsA7CAZUQIF58AffYMOKARlXwBlSlGlADeAJyTksALnwAjYsUIBfPmw5cATHwCi48cXFiAtkjhxISOQnHUIa1u05QAzHwAixKkjFkw4HKKymro6IRIIPgIAUz4+obGomYWVnIwtMRmIAAW9vgA7khUUQWGNACEMKFAA

@czabaj
Copy link

czabaj commented Mar 9, 2024

Correct, use tagged variants, e.g. in my case, I needed interop with TypeScript enum and this chapter in the blog post goes to the point.

I then used coercion instead of the methods generated by @deriving(jsConverter)

The only drawback is that the conversion from JS -> Variant is missing, you cannot coerce e.g. int :> myType so you have to write the ...fromJs part yourself.

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

Successfully merging this pull request may close these issues.

None yet

4 participants