Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 1770

Unnecessarily strong type constraints with polymorphic variants (when using `React.useState`?)

$
0
0

I don’t think my first example is an error; I wrote it that way deliberately to make the typechecker infer the type of x in f to be ref<[> #b]>, and the type of x in g be [> #a]. That way, f can accept something more than #b, while inside g, the switch expression is still exhaustive. I believe this is because the type checker unifies the types with the most general types.

That is why I am puzzled with the second example, as I wrote it (almost) the same way as I have done with the first example—so I think the second example is misleading for me. What’s more puzzling is that the following type checks:

let f = v => {
  let (_x, setX) = React.useState(() => #b)
  setX(_ => v)
}
let g = () => {
  let (x, _setX) = React.useState(() => #a)
  f(x)
  /* switch x {
  | #a => f(x)
  } */
}

and this too:

let f = v => {
  let (_x, setX) = React.useState(() => #b)
  setX(_ => v)
}
let g = () => {
  let x = #a
  switch x {
  | #a => f(x)
  }
}

Viewing all articles
Browse latest Browse all 1770

Trending Articles