-
Notifications
You must be signed in to change notification settings - Fork 12
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
RFC for const generics #42
base: master
Are you sure you want to change the base?
Conversation
9d50946
to
de7209d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, the definition itself ends up pretty straightforward, the hard part is going to be the monomorphization implementation.
|
||
[motivation]: #motivation | ||
|
||
Some types have constants, specifically unsigned integers, as their definition (e.g. arrays and string arrays). Without `const generics` it is impossible to have `impl` items for all instances of these types. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some types have constants, specifically unsigned integers, as their definition (e.g. arrays and string arrays). Without `const generics` it is impossible to have `impl` items for all instances of these types. | |
Some types have constants, specifically unsigned integers, as a part of their definition (e.g. arrays and string arrays). Without `const generics` it is impossible to have a single `impl` item for all instances of these types. |
A simple example would be: | ||
|
||
```rust | ||
fn id<const SIZE: usize>(array: [u64; SIZE]) -> [u64; SIZE] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In most of the code samples usize
is used which is misleading because this type does not exist in Sway. Was the intention maybe to emphasize that any unsigned type can be used?
Even in that case, the proposal would be to go with u64
or other concrete types in examples and specify in the RFC which types can be used in const generics.
Or to mention on the top of the Guide-level explanation that usize
means any of the types: u8
, ...
Still, in this particular example even that is misleading because currently array size must be u64
.
fn id<const SIZE: usize>(array: [u64; SIZE]) -> [u64; SIZE] { | |
fn id<const SIZE: u64>(array: [u64; SIZE]) -> [u64; SIZE] { |
This also allows `impl` items such as | ||
|
||
```rust | ||
impl<const N: usize> AbiEncode for str[N] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
impl<const N: usize> AbiEncode for str[N] { | |
impl<const N: u64> AbiEncode for str[N] { |
|
||
[reference-level-explanation]: #reference-level-explanation | ||
|
||
This new syntax has three forms: declarations, instantiations and references. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new syntax has three forms: declarations, instantiations and references. | |
This new syntax has three forms: declarations, instantiations, and references. |
|
||
This new syntax has three forms: declarations, instantiations and references. | ||
|
||
"const generics declarations" can appear anywhere all other generic arguments declarations are valid: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"const generics declarations" can appear anywhere all other generic arguments declarations are valid: | |
"Const generics declarations" can appear anywhere all other generic arguments declarations are valid: |
``` | ||
6. Struct/enum support for const generics | ||
7. Function/method declaration; | ||
8. `impl` declarations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
8. `impl` declarations. | |
8. `impl` declarations; |
6. Struct/enum support for const generics | ||
7. Function/method declaration; | ||
8. `impl` declarations. | ||
9. Function/method reference; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
9. Function/method reference; | |
9. Function/method reference. |
|
||
[prior-art]: #prior-art | ||
|
||
This RFC is partially based on Rust's own const generic system: https://doc.rust-lang.org/reference/items/generics.html#const-generics, https://blog.rust-lang.org/inside-rust/2021/09/06/Splitting-const-generics.html, https://rust-lang.github.io/rfcs/2000-const-generics.html, https://doc.rust-lang.org/beta/unstable-book/language-features/generic-const-exprs.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This RFC is partially based on Rust's own const generic system: https://doc.rust-lang.org/reference/items/generics.html#const-generics, https://blog.rust-lang.org/inside-rust/2021/09/06/Splitting-const-generics.html, https://rust-lang.github.io/rfcs/2000-const-generics.html, https://doc.rust-lang.org/beta/unstable-book/language-features/generic-const-exprs.html | |
This RFC is partially based on Rust's own const generic system: | |
- https://doc.rust-lang.org/reference/items/generics.html#const-generics | |
- https://blog.rust-lang.org/inside-rust/2021/09/06/Splitting-const-generics.html | |
- https://rust-lang.github.io/rfcs/2000-const-generics.html | |
- https://doc.rust-lang.org/beta/unstable-book/language-features/generic-const-exprs.html |
|
||
[unresolved-questions]: #unresolved-questions | ||
|
||
1. What is the impact of changing the "method called algorithm"? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. What is the impact of changing the "method called algorithm"? | |
1. What is the impact of changing the "method call search algorithm"? |
|
||
1. What is the impact of changing the "method called algorithm"? | ||
|
||
# Future possibilities |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned above, implementing constraints like where N > 0
.
Rendered