From the documentation of ReScript React, I can declare a component like this:
module OptionalChildren = {
@react.component
let make = (~children: option<React.element>=?, ~children2: React.element=?) => {
<div>
{switch children {
| Some(element) => element
| None => React.string("No children provided")
}}
{switch children2 {
| Some(element) => element
| None => React.string("No children provided")
}}
</div>
}
}
Both children
and children2
evaluate to option<React.element>
type? So, what's the difference? Is latter just the short form of former?
The first form is explicit, since
children
will actually be anoption<_>
. The second form will emit the warning:The word "must" is a bit odd, since clearly it does compile without it. But I believe the motivation for it is type annotation accuracy, to avoid confusion. For example, what if your intention is actually
option<option<React.element>>
? How would you interpret seeing justoption<React.element>
?Note also that it's possible to provide a default argument,
~children: React.element=React.null
for example, which adds to the complexity of the construct.