You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature request] zero-cost binding to tagged JS objects #5207
I explored the various ASTs on astexplorer.net and saw many possibilities. It would be fantastic to use pattern matching when dealing with trees parsed by parser libraries
But in my understanding, that requires converting JS objects to a record and is converted to a ReScript internal representation at runtime.
this feels unnecessary overhead when writing some bindings for a parser library. Because each item in the tree is already a tagged object.
I think this can be a more ergonomic approach when writing bindings for parsers.
Currently, I have to rely on a 3rd party ppx like decco for this kind of work. Or please let me know if there is a better way I am not aware of
Reacted by Iha Shin (신의하), Jason Kim, Gabriel Nordeborn, Florian Hammerschmidt, Martin Kinoo, celso, Ben Styles, Sergey Samokhov, Diogo Mafra, Seunguk Lee and 1 more
This would be useful in other situations too, I jump through a lot of hoops to bind to Slate's operation set using string matching and %identity externals.
The operations are 9 objects that use a shared type field as a tag. Some have overlapping fields but that’s the only unique one across all operations and I use it to direct operations to one of 9 identity functions. This lets me wrap the value in a variant (thus adding a second layer of tagging at runtime).
This type of functionality would be incredibly valuable indeed. The type of structure described is used a lot in JS-land, and hassle-free bindings to those types of structures would open up interesting possibilities with ReScript (like dealing with ASTs which ReScript in theory is very very good at, but that's painful/close to impossible to do in a sane way now that every single variant needs to be manually mapped at runtime).
I wonder though if it'd be better modelled as a polyvariant? Since Flow/TS models these types of things structurally, I think it'd be valuable to model at least the tag itself structurally with a polyvariant, rather than with a normal variant.
It looks like polymorphic variants right now are translated to almost what you'd expect:
leta=#hello({ "world": 1 })
vara={NAME: "hello",VAL: {world: 1}};
To the point that providing a stable encoding (possibly via an @tagged annotation somewhere) where the contents are inlined in the object, and the discriminating field name is specified upfront, could be enough to start exploring this:
Of course this means that @tagged polymorphic variants without arguments will still have the shape { type: "name" }, but people are doing that on the other side of the type-system already.
From similar work I did on Caramel, (where #hello(1) becomes {hello, 1} in Erlang/Elixir), using polymorphic variants makes this rather natural. Also not a TypeScript user these days but I could see how writing bindings with this could be easier.
Reacted by Hyeseong Kim, Gabriel Nordeborn, Martin Kinoo, celso, Mathieu H, Ben Styles, Juang Wiantoro, Tomi Saarinen and Léo Le Bras
Seems a must have !
I also though we should cover other JSON shape with variant, in a more general way.
Some rough ideas about it, just writing them for the sake of it:
we can also have control on the NAME/VAL keys with an other attribute, e.g:
where that @keys attribute have a tuple as argument to define the key. Doesn't seems really helpfull though, your approach seems better.
In the other case we have full control other the JSON shape (e.g: request from an API we have), we can also use non-polymorphic parametric variant for tinier payload and I assume more efficient pattern matching, but still define keys for clarity. It will be a bit more complex if we expect to manage different parameters shapes . The current behavior of regular parametric variant is:
It will have a similar result than the polymorphic variant with tag, but with a slight performance gain (that I still assume) in exchange of a more complex syntax.
We can add some sugar in the case we have the same shape:
Again, your idea feel better, even if I would prefer using regular variant.
For some extreme case, we can also imagine that the tagged/keys attribute can be a polymorphic variant for the sake of covering all use case, for example retro-compatibility with a name change on an API.. but things get more complex and probably not that useful, I don't think it worth it.
We love the syntax proposed by @Ostera, but since the current poly vars type cannot contain inline record definitions, I assume there will be some semantic changes for it.
It feels natural to have tag = "TAG" always present in regular variants and to tell the compiler to use a custom tag.
// Assume here is an implicit directive// @tag("TAG")typet=
| Foo({ foo: string })
| Bar({ bar: int })
Currently, we are saying users "don't rely on the internal representation".
However, It may be better to make the internal representation of a regular variants more predictable rather than treating this as special case.
Pros:
More readable JS output
Reduce the learning curve
Ergonomic bindings
Rust's serde_enum provides a good summary of the predictable representation.
Activity
TheSpyder commentedon Jul 1, 2021
This would be useful in other situations too, I jump through a lot of hoops to bind to Slate's operation set using string matching and
%identityexternals.bobzhang commentedon Jul 1, 2021
@TheSpyder the link is broken
TheSpyder commentedon Jul 1, 2021
I wasn’t linking to my bindings, those aren’t public (yet). The link is to the source types I’m binding to. The specific line that defines the type is here but it needs context of the rest of the file:
https://github.com/ianstormtaylor/slate/blob/4945a1a27505f59805bbbb630d8e22e47b1f29e5/packages/slate/src/interfaces/operation.ts#L138
The operations are 9 objects that use a shared
typefield as a tag. Some have overlapping fields but that’s the only unique one across all operations and I use it to direct operations to one of 9 identity functions. This lets me wrap the value in a variant (thus adding a second layer of tagging at runtime).zth commentedon Jul 2, 2021
This type of functionality would be incredibly valuable indeed. The type of structure described is used a lot in JS-land, and hassle-free bindings to those types of structures would open up interesting possibilities with ReScript (like dealing with ASTs which ReScript in theory is very very good at, but that's painful/close to impossible to do in a sane way now that every single variant needs to be manually mapped at runtime).
I wonder though if it'd be better modelled as a polyvariant? Since Flow/TS models these types of things structurally, I think it'd be valuable to model at least the tag itself structurally with a polyvariant, rather than with a normal variant.
leostera commentedon Jul 28, 2021
It looks like polymorphic variants right now are translated to almost what you'd expect:
To the point that providing a stable encoding (possibly via an
@taggedannotation somewhere) where the contents are inlined in the object, and the discriminating field name is specified upfront, could be enough to start exploring this:Or for the long form:
Of course this means that
@taggedpolymorphic variants without arguments will still have the shape{ type: "name" }, but people are doing that on the other side of the type-system already.From similar work I did on Caramel, (where
#hello(1)becomes{hello, 1}in Erlang/Elixir), using polymorphic variants makes this rather natural. Also not a TypeScript user these days but I could see how writing bindings with this could be easier.kinooyume commentedon Aug 4, 2021
Seems a must have !
I also though we should cover other JSON shape with variant, in a more general way.
Some rough ideas about it, just writing them for the sake of it:
NAME/VALkeys with an other attribute, e.g:where that
@keysattribute have a tuple as argument to define the key. Doesn't seems really helpfull though, your approach seems better.We can have the same attribute to change keys, if we have different shapes:
It will have a similar result than the polymorphic variant with
tag, but with a slight performance gain (that I still assume) in exchange of a more complex syntax.We can add some sugar in the case we have the same shape:
Again, your idea feel better, even if I would prefer using regular variant.
For some extreme case, we can also imagine that the
tagged/keysattribute can be a polymorphic variant for the sake of covering all use case, for example retro-compatibility with a name change on an API.. but things get more complex and probably not that useful, I don't think it worth it.cometkim commentedon Aug 25, 2021
We love the syntax proposed by @Ostera, but since the current poly vars type cannot contain inline record definitions, I assume there will be some semantic changes for it.
It feels natural to have tag = "TAG" always present in regular variants and to tell the compiler to use a custom tag.
Currently, we are saying users "don't rely on the internal representation".
However, It may be better to make the internal representation of a regular variants more predictable rather than treating this as special case.
Pros:
Rust's serde_enum provides a good summary of the predictable representation.
cannorin commentedon Mar 18, 2022
I'm creating a tool to generate ReScript bindings from
.d.tsfiles, and this would be nice to have for consuming TS discriminated unions.cometkim commentedon Nov 10, 2022
I'm thinking of a PPX syntax that can be introduced without breaking changes.
ex)
cometkim commentedon Apr 11, 2023
I just confirmed this works in ReScript v11
input code:
And the output:
The result is exactly what I want! Thanks, @cristianoc
I will write some bindings for popular parser libraries and report back if I run into any problems.
cometkim commentedon Apr 11, 2023
The only problem I've noticed so far is that when I wanna reuse a tag name in the code, I have to hardcode it.
zth commentedon Jul 16, 2023
I think this can be considered solved via these recently merged features:
Please feel free to open new issues for anything missing for this workflow to work well.
DZakh commentedon Jul 16, 2023
Also, if you need a parser library, it'll be well supported in
rescript-struct@5https://github.com/DZakh/rescript-struct/blob/f6dfc93/CHANGELOG_NEXT.md#opt-in-ppx-support