E4141#
This form of application is invalid for the argument, because it is not declared as optional.
This error happens when you try to forward an optional value to an argument to a function. This is only allowed when the argument is declared as optional argument. Even if the argument is declared as a labelled argument with default value, it is not allowed to forward an optional value to it.
错误示例#
fn f(opt~ : Int = 4) -> Unit {
println("opt: \{opt}")
}
fn main {
let opt = Some(42)
f(opt?) // Error: This form of application is invalid for argument opt~, because it is not declared with opt? : _.
}
建议#
If you have control over the function you wish to call, you can change the argument to be optional.
fn f(opt? : Int) -> Unit {
let opt = match opt {
Some(opt) => opt
None => 4
}
println("opt: \{opt}")
}
However, if you does not have control over the function, you can unwrap the optional value before passing it to the function.
fn main {
let opt = Some(42)
let opt = match opt {
Some(opt) => opt
None => 4
}
f(opt~)
}