Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upSlice with size known at compile time should be coerced to array #1772
Comments
nrc
added
the
T-lang
label
Oct 17, 2016
This comment has been minimized.
This comment has been minimized.
|
I've been thinking about a more general solution to this.
That's a little surprising in my opinion. |
This comment has been minimized.
This comment has been minimized.
|
For I call it "value refinement", that is, you've refined a type to a single value (any pattern can technically be a refinement and you can even have full subtyping with "pattern-refined types" if you wanted to). But it's one of those typesystem extensions that are "less mainstream" than, say, HKT, or even GADTs. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@est31 No, those are nowhere near allowing |
This comment has been minimized.
This comment has been minimized.
burdges
commented
Oct 18, 2016
|
I'm happy to learn about the arrayref crate because lacking this gets annoying. |
burdges
referenced this issue
Oct 18, 2016
Closed
associated-constants can not be used in constant expressions #34344
This comment has been minimized.
This comment has been minimized.
burdges
commented
Oct 18, 2016
|
I think should go read about |
This comment has been minimized.
This comment has been minimized.
|
@burdges I've wondered about it as well and talked with @eddyb on IRC. In fact, not the Index trait is used here, but the Range struct. The problem is that the Range struct only has fields for the borders, whose value is only known at runtime. @eddyb is proposing that the issue is left until there is a generic way to work even with these values. However, he confirmed that something like |
This comment has been minimized.
This comment has been minimized.
burdges
commented
Oct 18, 2016
|
I see. It's an issue of there being too many things like this, so they should be dealt with using something more general like #1657, as opposed to many little syntax hacks. |
This comment has been minimized.
This comment has been minimized.
VictorKoenders
commented
Oct 13, 2018
|
I'd love to have this feature, especially with firmware development where compile-time checks like this are crucial |
This comment has been minimized.
This comment has been minimized.
SoniEx2
commented
Oct 13, 2018
|
So uh, |
This comment has been minimized.
This comment has been minimized.
VictorKoenders
commented
Oct 14, 2018
•
|
It's more like a specialization for indexing a slice by
|
est31 commentedOct 16, 2016
You should be able to write stuff like:
If subslice notation (
[]with a range inside) is used on a slice or array with the borders known at compile time, it should be coerced to an array.There is a stackoverflow thread with a list of workarounds, and someone has even done a crate for the problem, but it should be solved inside the language, as it doesn't really feel natural.
Also, if one end is unspecified, it should allow for values only known at runtime for the specified end, and infer the unspecified end from the type of the function. Example code: