Why this binding can not be compiled?

image
image
Also, i am very confused about when to use option or not?

@react.component
let make = (~value: string=?) => {
    React.null
}

@react.component
let make = (~value: option<string>=?) => {
    React.null
}

It turns out that @uncurry can not used here.

~onResize: option<(~width: int, ~height: int) => unit>=?,

After using rescript for almost one and a half year, I still get blocked with optional labeled paramters in react… Could anybody give me an more clear explaination?

You have to think about your point of view, are you reasoning from the body of the function or from the call site?

Here you are working on a binding, so you don’t have access to the body of the function, you’re only interested in the call site. On the call site, ~onResize parameter either takes a function as input or is not used at all, to define this you’d use:

~onResize: (~width: int, ~height: int) => unit=?,

So that’s how you have to think if you’re writing bindings, module signatures or interface (.resi) files.

Now if you’re writing a function and you’re annotating the parameters, you have to think from the point of view of the function body, what’s the type of this parameter when used?

Then it depends, if you don’t give it a default value, it’ll indeed be an option:

~onResize: option<(~width: int, ~height: int) => unit>=?,

But you could also give it a default value like this:

~onResize: (~width: int, ~height: int) => unit=(~width, ~height) => Js.log2(width, height),

Then as you can see, from the point of view of the function body, the type of this parameter can be different, but from the call site, it’ll be used the same way in both cases so its signature in the .resi file will remain the same:

~onResize: (~width: int, ~height: int) => unit=?,

Am I clear enough? ^^

1 Like

@tsnobip Hi, thanks, you mean only using option when we define the function with body?

yes you could say that, only wrap in option when you annotate an optional function parameter with no default value.

1 Like

See also Type for function with optional named args different? - #2 by yawaramin

1 Like