-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Array/Slice custom index type #1830
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
Comments
Actually, my proposal starts with a use case this doesn't solve. And now that I think about it. This is two proposals
|
This is a bad idea because each function that accepts slices would now have to have multiple compiled versions for the multiple types of slices. If you want this do it manually, with arrays. |
It doesn't have to be that way. You can always have small slices coerce into bigger ones. That way you can have general-purpose functions accept I think this would play well with #3806. You could have special slices for arrays with a maximum size given by a range type, and it would give the compiler room to use spare values for metadata. In fact, that would help with the problem of requiring an extra bit for the size type, which would only be the case if you need to use the whole power-of-two range for indices (and if so, you could still store other things in the extra space you get with one more bit). |
I want to tackle the proposal #3806 and figure out a decision on that one before this one. |
I was searching about typed indices and I think this issue is exactly what I'm trying to do. To give some context, I have an algorithm that has two arrays, each with their respective indices. Only, it would be very easy to mix both indices and access one of the arrays with the wrong index. For example: var array1 = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var index1 = 7; // the 8th element in array1, fine because array1 has a length of 10
var array2 = [_]u8{ 1, 2, 3, 4, 5 };
var index2 = 2; // the 3rd element in array2, fine because array2 has a length of 5
array2[index1] // woops, I used the wrong index with the wrong array I would love it if there was a way to make a custom index type to associate an array with a specific type of index such that indexing an array with the wrong type of index will result in a compile error, such as: var array1 = [_:Index1]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // with index type 'Index1'
var index1 : Index1 = 7; // index of type 'Index1'
var array2 = [_:Index2]u8{ 1, 2, 3, 4, 5 }; // with index type 'Index2'
var index2 : Index2 = 2; // index of type 'Index2'
array2[index1] // ERROR: array2 expects Index2 type index but Index1 type is used Any plans to have such a feature eventually? |
See also #14926 which is more or less a duplicate of this but is a bit more detailed on the syntax and has some discussion too. |
search keyword: |
One thing I end of doing sometimes is constructing a table that maps all
u4
values to something else. I tend to do this with arrays:And then, when I wanna access items in my array, I ensure that I have a
u4
type before I index:As long as I ensure that I index with a
u4
this is 100% safe. Sadly the compiler doesn't enforce this.Here are a few benitfits that this kind of feature could provide:
[u4]u8
(this is made up syntax) would be the same as this struct:struct { ptr: [*]u8, len: u4 }
. This should also work for enum arrays:[SomeEnum]u8 == struct { ptr: [*]u8, len: @TagType(SomeEnum) }
. This would work well with the replace @setRuntimeSafety with @optimizeFor #978, as a struct marked@optimizeFor(.Small)
would allow data to be packed with the length of the slice:Ofc, this might not require a feature. I can imagine a userland implementation that could give me all these benifits (except for the fact that I'd have to call
.at(index)
instead of[index]
)The text was updated successfully, but these errors were encountered: