-
Notifications
You must be signed in to change notification settings - Fork 28
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
Support polymorphic variants? #39
Comments
In case someone is searching for the same thing, it is possible to do this by using custom codecs. Here's one example that converts locale strings to polyvariants
And usage in the type definition
|
There is a less verbose way to do this. According to BuckleScript doc,
Those functions can be used as the base for conversion. In
Then, we can use the helpers like this in
|
This can be made more convenient with functor. Add this to
Then, you can
|
@kittipatv Nice. That is indeed much more elegant way to do it. |
However note that jsConverter does not support polymorphic variants with payloads. |
Just to inform: // any polyvar like
let c= `color; // will compile to just a normal string in Javascript
var c = "color" source: |
I'm happy to work on that @ryb73. Thanks |
Thanks @davesnx! There are a couple general tips here: #25 (comment) #6 (comment) |
@kittipatv is there a good workaround for polyvariants with variable tags? E.g., type t = [#ENTERS | #REGULAR | #RETURNS | #FutureAddedValue(string)] Current workaround I've used is just to manually encode these cases: module Jump = DeccoHelper.MakePV({
@bs.deriving(jsConverter)
type t = [#ENTERS | #REGULAR | #RETURNS | #FutureAddedValue(string)]
let tToJs = (val: t): string => switch val {
| #ENTERS => "ENTERS"
| #REGULAR => "REGULAR"
| #RETURNS => "RETURNS"
| #FutureAddedValue(value) => value
}
let tFromJs = (s: Js.String.t ): option<t> => switch s {
| "ENTERS" => Some(#ENTERS)
| "REGULAR" => Some(#REGULAR)
| "RETURNS" => Some(#RETURNS)
| other => Some(#FutureAddedValue(other))
}
let name = "Jump"
}) |
Last version v1.4.0 supports polyvariants after merging #64 Let me know if you found any issue with that |
I'm quite confused by this, I must be missing something obvious, but v1.4.0 only successfully decodes arrays to poly variants, not strings. So the following fails: {
"format": "abc"
} @decco.decode
type t = {
format: [#abc]
} But the following works: {
"format": ["abc"]
} @decco.decode
type t = {
format: [#abc]
} Can this be right? |
@benadamstyles This is correct. I think what you're looking for is described in #36 |
@ryb37 Thanks for the quick response! I still don't understand – why are polyvariants, which are single values, being encoded to arrays? |
Basically it's because they're not necessarily single values. For example [@decco] type v = `something(int, string);
`something(123, "abc") |> v_encode; /* result: ["something", 123, "abc"] */ Part of me wishes in retrospect I'd have made it so that variants without |
Ok thanks, I understand now. I'm not sure how #36 would help because that's about renaming, if I understand correctly, whereas I need a completely different representation... but if that's a breaking change I don't see any good way forward so I'll stick with manual decoders for now. Thanks again for the quick responses! |
Writing this
gives an error: "This syntax is not yet handled by decco"
I assume this is not surprising to anyone. Just creating a ticket to show my interest in this feature. :)
The text was updated successfully, but these errors were encountered: