Last active
November 21, 2018 16:33
-
-
Save remitbri/4998a57fb87ee53245383bf0d51c71bb to your computer and use it in GitHub Desktop.
polymorphic variant typing problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[@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 | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So… the correct code is