Splitting a tuple

Hey. Is it possible to extract a subpart of a tuple to get another tuple?

EDIT: I guess it is possible to do something with std.meta.fields, but I’m wondering if I’m missing a more obvious approach, and if not, what is the cost of that kind of type manipulations at runtime?

ok, I found a way to do what I want. For the record:

/// stack A and B in parallel
fn Par(comptime A: type, comptime B: type) type {
    return struct {
        // resulting IO are concat of A and B's IO
        pub const Input = A.Input ++ B.Input;
        pub const Output = A.Output ++ B.Output;
        a: A = A{},
        b: B = B{},

        pub fn eval(self: *@This(), input: std.meta.Tuple(&Input)) std.meta.Tuple(&Output) {
            // I know the types of the two sub-tuples, so it's fairly easy to declare this
            var input_a: std.meta.Tuple(&A.Input) = undefined;
            var input_b: std.meta.Tuple(&B.Input) = undefined;
            // then I just have to copy
            inline for (input, 0..) |v, i| {
                if (i < input_a.len) {
                    input_a[i] = v;
                } else {
                    input_b[i - input_a.len] = v;
                }
            }
            return self.a.eval(input_a) ++ self.b.eval(input_b);
        }
    };
}
1 Like