E4163#
Local parameters are not local.
#locals is an attribute that aims to inline the given function parameter. This
error is raised when the given parameter is not a ‘local’ one.
错误示例#
///|
#locals(f)
pub fn[A, B] map(
arr : ArrayView[A],
f : (A) -> B,
target? : Array[B] = [],
) -> Array[B] {
match arr {
[hd, .. tl] => {
target.push(f(hd))
map(tl, f, target~) // The parameter is not local: it escapes.
}
[] => target
}
}
建议#
Apply locals to local functions only.
///|
#locals(f)
pub fn[A, B] map(
arr : ArrayView[A],
f : (A) -> B,
target? : Array[B] = [],
) -> Array[B] {
loop arr {
[hd, .. tl] => {
target.push(f(hd))
continue tl
}
[] => target
}
}