Skip to content

Instantly share code, notes, and snippets.

@remitbri
Last active November 21, 2018 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remitbri/4998a57fb87ee53245383bf0d51c71bb to your computer and use it in GitHub Desktop.
Save remitbri/4998a57fb87ee53245383bf0d51c71bb to your computer and use it in GitHub Desktop.
polymorphic variant typing problem
17 ┆ jsStuff =>
18 ┆ switch (beeTypeFromJs(jsStuff)) {
>19 ┆ | Some(output) => output
20 ┆ | None => `baa
21 ┆ };
This has type:
beeType
But somewhere wanted:
stuff4
The first variant type does not allow tag(s)
`caa, `cii, `coo, `cuu, `daa, `fii
[@bs.deriving jsConverter]
type beeType = [ | `baa | `bii | `boo | `buu];
[@bs.deriving jsConverter]
type ceeType = [ | `caa | `cii | `coo | `cuu];
[@bs.deriving jsConverter]
type deeType = [ | `daa];
[@bs.deriving jsConverter]
type feeType = [ | `fii];
type stuff4 = [ beeType | ceeType | deeType | feeType];
type stuff2 = [ beeType | ceeType];
let simplifiedFromJs: string => [ stuff4] =
jsStuff =>
switch (beeTypeFromJs(jsStuff)) {
| Some(output) => output
| None => `baa
};
/* stuff2FromJs and stuff4FromJs are what I really want to do */
let stuff2FromJs: string => [ stuff2] =
jsStuff =>
switch (beeTypeFromJs(jsStuff)) {
| Some(output) => output
| None =>
switch (ceeTypeFromJs(jsStuff)) {
| Some(output) => output
| None => `baa
}
};
let stuff4FromJs: string => [ stuff4] =
jsStuff =>
switch (beeTypeFromJs(jsStuff)) {
| Some(output) => output
| None =>
switch (ceeTypeFromJs(jsStuff)) {
| Some(output) => output
| None =>
switch (deeTypeFromJs(jsStuff)) {
| Some(output) => output
| None =>
switch (feeTypeFromJs(jsStuff)) {
| Some(output) => output
| None => `baa
}
}
}
};
@remitbri
Copy link
Author

So… the correct code is

let simplifiedFromJs: string => [ stuff4] =
  jsStuff =>
    switch (beeTypeFromJs(jsStuff)) {
    | Some(output) => (output :> stuff4)
    | None => `baa
    };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment