-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 static generic parameters #56
Conversation
Other uses worth mentioning include the ability to define compile-time sizes of small buffers and other data-structures. Some data types need flexibility but they don't need it at a call site, or a call site knows enough of its domain to make optimization choices. See the various |
For an example of this, see how Servo defines a bunch of |
|
||
```rust | ||
fn concatenate<T, n: uint, m: uint> | ||
(x: Vector<T, n>, y: Vector<T, m>) -> Vector<T, n + m> |
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.
I could be wrong, but this would probably require a dependent type system. This is far too big a problem to be tackled by Rust on top of all the other challenges it is taking on.
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.
Technically this whole RFC is proposing dependent types (with or without the algebra). These algebraic expressions are just like constant expressions, and aren't anything like full dependent types in terms of complexity.
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.
Ah, so there is a technical term for this type system feature. Thanks for pointing that out. I've read a little more on that topic now and it looks like a full dependent type system is a bit of an overkill.
Having at least something comparable to what C++ provides would still be an asset though. Do you think that's possible?
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.
By the way I'm very much in favour of generic static parameters, I just think it might be worth defining the limits of this proposal.
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.
i hope this goes through . fixed point arithmetic is another use case, also shift values for compressed/relative pointers
Mentions the relation to a dependent type system.
I've included your feedback into the RFC. |
|
||
``` | ||
|
||
The syntax `<n: int>` closely resembles the syntax for type parameters with |
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.
Have you thought of using the static
keyword to make this clearer?
fn add_n<static n: int>(x: int) -> int
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.
Yes. That would be my favourite alternative to the syntax proposed, if it is necessary or desired to mark these parameters. I should probably explicitly add this to the RFC.
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.
Yeah I think it would remove any ambiguity. That way you would not need to know if int
was a trait or type. int
is easy, but user defined types could be more difficult.
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.
Hm… I mostly thought about distinguishing them visually, since the static parameters will be lowercase, while the type parameters are uppercase. But then if the trait bounds and static types look alike this becomes less clear. I guess it makes sense to change it.
Awesome! I would also add that static generic parameters could be used for implementing range and mod types like in Ada as library types: type Score is range 1 .. 10;
type Hours is mod 24; |
Done. I suspect that there are more things to think about in the detailed design section and it should probably be fleshed out in some more detail. But I am not entirely sure, how exactly to approach this, i.e. what kind of caveats and corner cases are still to be considered. Or is this already detailed enough for an RFC? |
Regarding arbitrary code execution during the compilation: did not Rust already jumped the gun with procedural macros ? |
@matthieu-m that is true |
While desirable, a feature such as this has far-reaching interactions with the rest of the type system. The team has decided to postpone this to after 1.0, so I'm tagging this as postponed and closing for now. |
Fix typo in join example
Release cycle improvements
This is a rough draft to see whether something like this should be done. I'd appreciate feedback on how the technical details could look like, since I do not know a lot about the compiler internals myself.