1

I have a function that accepts a struct

fn foo(args: anytype) void {
    const ArgsType = TypeOf(args);
    const args_type_info = typeInfo(ArgsType);

    switch (args_type_info) {
        .Struct => { // do work
        },
        else => @compileError("tuple or struct expected"),
}

I want to pass array elements as a tuple to this function, but the only way I know of is to write all elements manually

const SIZE = 3;
var array = [_]u8{0} ** SIZE;
foo(.{ array[0], array[1], array[2] });

Is there a way to do this automatically in comptime so that if I change SIZE to 100 I don't have to type all elements by hands?

I know I can extend switch to take .Array but I'm wondering if I can do it with just .Struct

KindFrog
  • 358
  • 4
  • 17

1 Answers1

2

std.meta.Tuple(slice) could treat this, however, the latest compiler cannot do this because of a bug.

https://github.com/ziglang/zig/issues/15822

Moreover, comptime range slice1 generation is also difficult because of another bug.

https://github.com/ziglang/zig/issues/6936


1: see https://github.com/nektro/zig-range/blob/master/src/lib.zig

fumiya.f
  • 255
  • 3
  • 13