#How do I access an anonymous tuple value?

1 messages · Page 1 of 1 (latest)

tawdry glen
#

Consider this program

pub fn main() void {
    const A = struct { u16, u32 };
    const B = @TypeOf(.{ u16, u32 });

    // prints "A: struct { u16, u32 }"
    std.debug.print("A: {s}\n", .{@typeName(A)});

    // prints "B: struct { comptime type = u16, comptime type = u32 }"
    std.debug.print("B: {s}\n", .{@typeName(B)});

    // prints "u16"
    std.debug.print("{s}\n", .{@typeName(@typeInfo(A).@"struct".fields[0].type)});

    // prints "type"
    std.debug.print("{s}\n", .{@typeName(@typeInfo(B).@"struct".fields[0].type)});

    assert(@typeInfo(A).@"struct".fields[0].type == u16); // OK
    assert(@typeInfo(B).@"struct".fields[0].type == u16); // Not OK
}

const std = @import("std");
const assert = std.debug.assert;

I guess the difference is that for A, u16 is the type of the field, while for B the field type is type and the default value is u16. How do I get to the u16 in B? I can't seem to be able to figure it out.

warped dagger