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: Allow numeric generics to be referenced and add map #997

Merged
merged 4 commits into from
Mar 18, 2023

Conversation

jfecher
Copy link
Contributor

@jfecher jfecher commented Mar 16, 2023

Related issue(s)

Resolves #972

Description

Summary of changes

Allows numeric generics to be referenced like normal values. This resolves a common stumbling block for users when writing a function like the following:

fn foo<N>(array: [Field; N]) {
    for i in 0 .. N {
        ....
    }
}

Previously, we would error that there is no N in scope (since N is a type and not a value). With this PR, this code works as expected.

Notably, this PR now allows us to write the array map function and add it to our stdlib. This was not possible previously due to the syntax limitations placed on array-length expressions which prevented us from initializing an array using let mut ret = [0; array.len()];. This has been reworked slightly to reuse the same array length evaluation mechanism types use, which notably now allows us to use numeric generic type variables.

Dependency additions / changes

Test additions / changes

Added a test for the new array map function in higher_order_functions.

Checklist

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt with default settings.
  • I have linked this PR to the issue(s) that it resolves.
  • I have reviewed the changes on GitHub, line by line.
  • I have ensured all changes are covered in the description.

Documentation needs

  • This PR requires documentation updates when merged.

We should document numeric generics are now useable like any other Field variable. Additionally, the new map method for arrays will need to be documented.

@kevaundray kevaundray added this pull request to the merge queue Mar 18, 2023
@kevaundray kevaundray merged commit 34eab32 into master Mar 18, 2023
@kevaundray kevaundray deleted the jf/allow-numeric-generics-to-be-referenced branch March 18, 2023 13:05
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.

Allow referencing numeric generics as values
2 participants