本頁尚未翻譯,以下為英文原文。
#E0026: name defined more than once
Two items in one scope have the same name. Nabi has no overloading: rename one of them, or, for functions that differ in how many arguments they take, keep one and give its last parameters defaults (b: i32 = 0).
nabi
fn area(w: i32) -> i32 { w * w }
fn area(w: i32, h: i32) -> i32 { w * h }
error[E0026]: function `area` is defined more than once --> main.nabi:2:1 | 1 | fn area(w: i32) -> i32 { w * w } | -- first defined here 2 | fn area(w: i32, h: i32) -> i32 { w * h } | ^^ defined again here | = help: rename one of them, e.g. `area2`; there is no overloading, so to take fewer arguments keep one and give its last parameters defaults, as in `b: i32 = 0`
Keep one function and give its last parameter a default:
nabi
fn area(w: i32, h: i32 = 0) -> i32 { if h == 0 { w * w } else { w * h } }
println(f"{area(3)} {area(2, 5)}");
9 10
Related: E0038 variable named like a nested function, E0032 conflicting imports
Found a mistake? Edit this page on GitHub. Last updated