The following works (ReScript Playground):
let f = v => {
let x = ref(#b)
x := v
}
let g = () => {
let x = #a
switch x {
| #a => f(x)
}
}
but the equivalent code using React.useState does not (ReScript Playground):
let f = v => {
let (_x, setX) = React.useState(() => #b)
setX(_ => v)
}
let g = () => {
let (x, _setX) = React.useState(() => #a)
switch x {
| #a => f(x) /* This has type: [#a]
But this function argument is expecting: [> #b]
The first variant type does not allow tag(s) #b */
}
}
I can explicitly annotate x: [> #a | #b]
in g
, but it makes the switch non-exhaustive, and moreover, it is not a general solution as x
can no longer be applied with other functions accepting different poly variants, e.g., [> #c]
.
What is causing this problem? How can I bypass this?