-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Allow slicing of tuples #4625
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
Interesting. So the start and end indexes would both have to be comptime known, and it would produce a new tuple type as a result. Sort of the "tuple version" of #863. |
Since tuples can contain heterogeneously typed fields, it does seem natural for slicing such a tuple to yield another tuple. This is consistent with the fact that concatenating and multiplying tuples always yields a tuple even when the fields of the tuple operands are typed homogeneously. However, this would mean special-casing the behavior of slicing, which today always yields a value of either type Would users be astonished if tuples comprising homogeneously typed fields did not slice the same way that any other currently sliceable type slices, given that such tuples are already permitted to coerce to arrays? Aside: Would this proposal also allow for the slicing of tuple pointers? |
For what it's worth, I have a use case that would benefit a lot from this. To make it short, my project defines a computation graph at compile time - each node of the graph has a It is doable with a few helper functions, but beeing able to use the slicing syntax on tuples would clarify the code a lot. For reference here are my helper functions // extracting a list of field types from a tuple. essentially the inverse of std.meta.Tuple
fn fieldTypes(T: type) [std.meta.fields(T).len]type {
const fields = std.meta.fields(T);
var types: [fields.len]type = undefined;
for (fields, 0..) |*f, i| {
types[i] = f.*.type;
}
return types;
}
// return type of the split function
fn Split(T: type, comptime pivot: usize) type {
// TODO check A is tuple
const types = fieldTypes(T);
return struct {
Tuple(types[0..pivot]),
Tuple(types[pivot..]),
};
}
// split a tuple at pivot
fn split(tuple: anytype, comptime pivot: usize) Split(@TypeOf(tuple), pivot) {
var result: Split(@TypeOf(tuple), pivot) = undefined;
inline for (tuple, 0..) |value, i| {
if (i < pivot) {
result[0][i] = value;
} else {
result[1][i - pivot] = value;
}
}
return result;
} |
Also, would it make sense to do the same to manipulate tuple types?
But maybe that would count as another proposal... |
TFW tuple types are tuples themselves. Massive inception gigabrain moment... 😆 |
Slicing has typically meant getting a reference to the original memory of the thing being sliced. If the proposal merely wants functionality to copy portions of a tuple, then I doubt slicing would be the correct syntax or nomenclature for it. In that case, helper functions in the standard library would suffice. |
This, along with the multiplication and concatenation operators would make it much easier to write code that manipulates tuples.
The text was updated successfully, but these errors were encountered: