-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Array length inference? #40269
Comments
This would have to be a part of rust-lang/rfcs#1931, you should leave a comment there perhaps. The reason is that whenever you have a generic parameter (lifetime / type / const), you need inference variables to instantiate, at least in the (slightly modified) HM typesystem we use. Now, we could just not provide a surface syntax for this, but I think reusing |
When I wrote the above it didn’t feel like an entirely new language feature, just a "simple" extension of the existing inference mechanism. But I can imagine how from an compiler internals point of view it could require most of the plumbing for const parameters in types. |
See counted-array :)
|
This is definitely something that should be part of the language spec - definitely not somewhere macros shine. A particular pain point with rust arrays comes when adding a member to a previously declared array or when declaring an array without knowing in that moment how long it will be. For example: const ARRAY: [u8; 2] = [1, 2]; Coming back to it later and adding Additionally, when sitting down to write code for the first time, it is often the case that you don't know until you finish writing it how many members your array will have. For example, const VALID_EXTENSIONS: [String, _] = [
"ext1",
"ext2",
"ext3",
// let's stop and think for a while. What other extensions could we include here?
// oh yes!
"ext4",
"ext5",
} Only having reached the end of the list does one know how many they'll have. Painful. |
@mqudsi In the general case, const generics will have to include However, in your example, it's also like rust-lang/rfcs#2010, in that the type is inferred from the RHS. |
The use-case for this seems incredibly clear. Am I understanding you all correctly when I interpret the counter-point as being that the meaning of the How about simply: const VALID_EXTENSIONS: [String] = [
"ext1",
"ext2",
"ext3",
"ext4",
"ext5",
} Or alternatively, introduce a new symbol that carries no problematic baggage and use that in place of Or am I just misunderstanding the objections? |
Given that this pretty clearly requires an RFC, I'm going to give it a close. |
It would be nice if
_
were allowed instead of an expression for the length in array types and array expressions, in cases where the length can be inferred based on context:rustc 1.17.0-nightly (b1e3176 2017-03-03)
In comparison, the item type in array expressions and array types can be inferred today:
The text was updated successfully, but these errors were encountered: