Uncurried return type in binding?

I’m trying to figure out how to create a binding for Jotai where the return type is uncurried.

Essentially I would like the binding type to look like this:

@module("jotai/utils")
external useUpdateAtom: t<'value, 'setValue> => ('value => unit) = "useUpdateAtom"

With the return type being 'value => unit

But as soon as I hit save the compiler curries the type into this:

external useUpdateAtom: (t<'value, 'setValue>, 'value) => unit = "useUpdateAtom"

And this doesn’t work with the library.

The way I’ve fixed it for now is to introduce a special return type:

type returnType<'a> = 'a => unit
external useUpdateAtom: t<'value, 'setValue> => returnType<'value> = "useUpdateAtom"

This works, but I’m wondering if there’s another more idiomatic way to achieve this?

Your way is fine, there is also Bind to JS Function | ReScript Language Manual

1 Like

Thanks for the link. It works fine with the . uncurrying, but I’m not able to make it work with the @uncurry decorator.

This works:
external useUpdateAtom: (t<'value, 'setValue>, . 'value) => unit = "useUpdateAtom"

But this doesn’t:
external useUpdateAtom: (t<'value, 'setValue>, @uncurry 'value) => unit = "useUpdateAtom"
It throws an error: Cannot infer the arity through the syntax, either [%@uncurry n] or write it in arrow syntax

Well it’s not a big issue, just trying to understand bindings and currying.

Thanks again!

The decorator works for input position arguments only. Since you want to return an uncurried function here, it doesn’t work.

1 Like

That makes sense, thanks for the explanation :+1:

By chance have you seen https://github.com/gaku-sei/re-jotai? Or was that insufficient for a particular use case?

Thanks for the link; I did see and star the project. But it turned out to be a little too complex for my understanding of bindings and ReScript when we needed Jotai. So I decided to build our own simpler bindings, mainly to get some exercise writing them. And we also use a couple of Jotai utils which aren’t covered by those bindings yet.

But you bring up a good point. I should consider contributing our util bindings to that project!

1 Like