Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: resolve generic length of slice inconsistency #4232

Closed
wants to merge 19 commits into from

Conversation

michaeljklein
Copy link
Contributor

Description

Resolve generic N parameters applying inconsistently to arrays and slices.

Problem*

Resolves #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.

@Savio-Sou
Copy link
Collaborator

Also resolves #2540?

github-merge-queue bot pushed a commit that referenced this pull request Mar 19, 2024
# Description

Replacement for #4232. Holding off
on closing it to possibly port fixes from it, but wanted to start fresh
because this approach is quite different.

## Problem\*

Resolves #4220

## Summary\*

- Removes `NotConstant`
- Separate syntax for slice literals: `&[..]`
- Separate AST/type constructors for slices

Notes:
- I removed several broken links that were blocking updating the docs:
+ A few were to a missing `reference` docs folder, likely from an
in-progress branch
+ One was to some `base_order_curve`, which has been moved to reference
the `BigInt` struct

## Additional Context



## Documentation\*

Check one:
- [ ] No documentation needed.
- [x] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: Jake Fecher <[email protected]>
Co-authored-by: jfecher <[email protected]>
@michaeljklein
Copy link
Contributor Author

replaced by #4504

TomAFrench pushed a commit that referenced this pull request Apr 3, 2024
# Description

Replacement for #4232. Holding off
on closing it to possibly port fixes from it, but wanted to start fresh
because this approach is quite different.

## Problem\*

Resolves #4220

## Summary\*

- Removes `NotConstant`
- Separate syntax for slice literals: `&[..]`
- Separate AST/type constructors for slices

Notes:
- I removed several broken links that were blocking updating the docs:
+ A few were to a missing `reference` docs folder, likely from an
in-progress branch
+ One was to some `base_order_curve`, which has been moved to reference
the `BigInt` struct

## Additional Context



## Documentation\*

Check one:
- [ ] No documentation needed.
- [x] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: Jake Fecher <[email protected]>
Co-authored-by: jfecher <[email protected]>
@TomAFrench TomAFrench deleted the michaeljklein/map-slice branch November 20, 2024 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Calling map on a slice panics
3 participants