noir
noir copied to clipboard
feat: resolve generic length of slice inconsistency
Description
Resolve generic N
parameters applying inconsistently to arrays and slices.
Problem*
Resolves https://github.com/noir-lang/noir/issues/4220
Summary*
In general, generic N
parameters are expected to be able to be instantiated to any specific Type::Constant(n)
or Type::NotConstant
, however these two cases have slightly different semantics.
For example, the map
definition uses the array length N
from the type to pick the size of the new array, but this isn't known for slices, leading to a panic:
// Apply a function to each element of an array, returning a new array
// containing the mapped elements.
pub fn map<U, Env>(self, f: fn[Env](T) -> U) -> [U; N] {
let first_elem = f(self[0]);
let mut ret = [first_elem; N];
for i in 1 .. self.len() {
ret[i] = f(self[i]);
}
ret
}
In a new approach fn foo<T, N?>(x: [T; N])
would be needed to operate on slices as well. This would prevent N
from being used as a numeric.
Additional Context
Documentation*
Check one:
- [ ] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] [Exceptional Case] Documentation to be submitted in a separate PR.
PR Checklist*
- [ ] I have tested the changes locally.
- [ ] I have formatted the changes with Prettier and/or
cargo fmt
on default settings.
Also resolves https://github.com/noir-lang/noir/issues/2540?
replaced by https://github.com/noir-lang/noir/pull/4504