I’m trying to create a function that depending on input types returns different output types using type params. But I’m not getting it to work, take a look at this playground:
So it seems like the type parameter is “decided” on the first call do api.do, so all subsequent usage of the function is no longer generic. I guess this is just how it works. But is there another way to achieve this, without creating two variables (as seen on row 37)?
Indeed, this is a limitation with the normal rules of polymorphism in ReScript. 'a gets unified with int, so then it cannot be unified with string.
There is a way around it, though. Instead of making 'a a type parameter of apiInstance, you need to annotate the do record field with an “explicit universally quantified” type annotation.
type apiInstance<'b> = {do: 'a. request<'a, 'b> => response<'a, 'b>}
// This ^^^
Here is a nice blog post that introduces explicitly-polymorphic types in a simple way: https://www.craigfe.io/posts/polymorphic-type-constraints. It is about OCaml, but like johnj mentioned it works the same. Maybe it will help you out!