Skip to content

Instantly share code, notes, and snippets.

@remitbri
Last active Nov 21, 2018
Embed
What would you like to do?
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

remitbri commented Nov 21, 2018

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