#E0052: 值不可调用
可以调用的是函数、闭包、fn 值,以及类型实现了 ops::Call 的值。检查这个名称指的是函数,而不是同名的值。
nabi
let total = 3;
println(f"{total(1)}");
error[E0052]: a value of type `i64` is not callable --> main.nabi:2:12 | 2 | println(f"{total(1)}"); | ^^^^^^^^
调用函数或闭包:
nabi
let total = |x: i32| x + 3;
println(f"{total(1)}");
4
自己定义的类型可以通过 ops::Call 变为可调用;错误的 help 会给出这个 impl。一个实参按原样传入,多个实参以元组传入:
nabi
struct Line { a: f64; b: f64; }
impl ops::Call<f64> for Line {
type Output = f64;
fn call(self, x: f64) -> f64 { self.a * x + self.b }
}
let l = Line { a: 2.0, b: 1.0 };
println(f"{l(3.0)}");
7
相关错误码:E0047 以错误的形式使用项
发现错误? 在 GitHub 上编辑此页 最后更新: