I’m switching an an optional value (settings). Settings has some fields which also have optional values. In my desired condition I want to make sure that settings is Some
but want any optional fields to be destructured, but not have to be Some
. I thought this is how ReScript has always been but maybe I’m missing something here. Code time -
In this code I reach my first case as expected, when choices is defined but the defaultLabel doesn’t have to be.
let defaultLabel = switch settings {
| Some({defaultLabel: Some(defaultLabel)}) => defaultLabel
| _ => "Date"
}
let component = switch settings {
| Some({choices}) =>
<ToolbarDropdown
choices
?size
toggle={choice => setSelected(_ => choice->Some)}
label={selected
->Option.flatMap(i => choices->Choice.find(i))
->Option.mapOr(defaultLabel, i => i.label)}
renderItem={({choice}) => choice.label->str}
/>
| _ => React.null
}
In this code I do not reach my first case as expected.
let component = switch settings {
| Some({choices, defaultLabel}) =>
<ToolbarDropdown
choices
?size
toggle={choice => setSelected(_ => choice->Some)}
label={selected
->Option.flatMap(i => choices->Choice.find(i))
->Option.mapOr(defaultLabel->Option.getOr("Value"), i => i.label)}
renderItem={({choice}) => choice.label->str}
/>
| _ => React.null
}
Shouldn’t my second switch statement work?