A doubt in documentation regarding 'Explicitly Passed Optional'


#1

Refer the link, https://reasonml.github.io/docs/en/function.html#explicitly-passed-optional.

I don’t find any difference between

let result = drawCircle(~color, ~radius=?payloadRadius, ());

and

let result = drawCircle(~color, ~radius=payloadRadius, ());

Here I assume that the function is defined as

let drawCircle = (~color, ~radius=?, ()) => {
setColor(color);
switch (radius) {
| None => startAt(1, 1)
| Some(r_) => startAt(r_, r_)
}
};

Since radius is already optional in the definition of function, even if payloadRadius is of type option, it is dealt with in the definition. What point is there in passing payloadRadius as

~radius=?payloadRadius


#2

It’s not “even if payloadRadius is of type option”. It has to be. Try passing e.g. a number to it; it’d fail to type check.

If you can repro your assumed behavior, it means your payloadRadius came from JS side and that you didn’t type it correctly!

Try this: https://reasonml.github.io/en/try.html?reason=DYUwLgBAJgTghgdwMIEsYGNQQLwQBQB+8UKArgM7YD8ANPgJT04B8EA3gFAQTkIpjoAFvmJlyTTtwA+EAHIB7AHYgW+AIx019LhBkBleQFsQeGAH0m2VqbN1z27gF8OjgNwcOoSDBwQDxvAAmencvCBhA30D3DlhEVAxQQlEKahg6PEZ3OOQ0TBMiOBJUiIysjwB6ACoIMEEQchUAMzgUYFj4XMSC9HlgeRhsDQhC4soYEI74vKSCXv7B4dGxNOD3KoqOIA


#3

Oh, one thing I missed was

When given in this syntax, radius is wrapped in the standard library’s option type

So its like: If one wants to pass an already wrapped option type of variable as an optional argument, you need to use syntax as follows

~radius=?payloadRadius

Thanks for so immediate a response