I’m not sure why this doesn’t work, could it be a compiler bug?
I could define add5 as add(~a=5)
but I’ve heard some talk about this feature being removed in the future so would ideally want to avoid it.
I’m not sure why this doesn’t work, could it be a compiler bug?
I could define add5 as add(~a=5)
but I’ve heard some talk about this feature being removed in the future so would ideally want to avoid it.
You need to use explicitly passed optional.
This works:
let add = (~a=?, ~b=?, ()) =>
a->Belt.Option.getWithDefault(0) + b->Belt.Option.getWithDefault(0)
let add5 = (~b=?, ()) => add(~a=5, ~b?, ()) // Notice the ~b?
What’s happening is that add
's ~b
parameter takes an int
, and add5
's ~b
parameter is option<int>
, thus the type error. By changing ~b=b
(or ~b
with punning) to ~b=?b
(or ~b?
with punning) you tell the compiler to not automatically wrap the value in an option
when it passes to add
, so you can now just use a plain option
value.
Wow, that’s confusing! Thanks