#Cant Reifying a Struct

1 messages · Page 1 of 1 (latest)

lilac flume
#

When trying to Reify a std.builtin.Strcut I get a weird behaviour:

pub const EnumFlags = enum(c_int) {
    FLAG_FULLSCREEN_MODE = 2,
    FLAG_WINDOW_RESIZABLE = 4,
    FLAG_WINDOW_UNDECORATED = 8,
    FLAG_WINDOW_TRANSPARENT = 16,
};

test "test enum to packed struct" {
    const bt = std.builtin;
    comptime var en: bt.Type.Enum = @typeInfo(EnumFlags).Enum;

    const fields: []const bt.Type.StructField = comptime blk: {
        var strcut_fields: [en.fields.len]bt.Type.StructField = undefined;

        for (en.fields) |field, i| {
            const sf = bt.Type.StructField{
                .name = field.name,
                .field_type = bool,
                .default_value = null,
                .is_comptime = true,
                .alignment = 0,
            };
            strcut_fields[i] = sf;
        }
        break :blk &strcut_fields;
    };


    const T: type = @Type(bt.Type { 
        .Struct = bt.Type.Struct {
            .layout          = bt.Type.ContainerLayout.Packed,
            .backing_integer = UIntOf(en.fields.len),
            .fields          = fields,
            .decls           = &.{},
            .is_tuple        = false,
        }
    });

    std.debug.print("\n", .{});
    std.debug.print("T is: '{}'\n", .{T});

    var t: T = T {
        .FLAG_FULLSCREEN_MODE = false,
        .FLAG_WINDOW_RESIZABLE = false,
        .FLAG_WINDOW_UNDECORATED = false,
        .FLAG_WINDOW_TRANSPARENT = false,
    };

    std.debug.print("t = {}\n", .{t});
}

It seems like the Interperter just quits and never try to compile. (seems like it is failing on something)

What am I doing wrong here?

I what a struct that would have fields with names that reflects the enum fields...

raven beacon
#

you can just make &.{} on .decls

#

also you can do
comptime var en = @typeInfo(EnumFlags).Enum;

lilac flume
raven beacon
#

whats the error?

lilac flume
#

the struct is something I cant use, see the log of the type: 'temp.test.test.test enum to packed struct__struct_2268'

#

this long thing is the name of the struct or something

#

and it contains the test name

raven beacon
#

reified types have these weird names

lilac flume
#

I cant use this type

raven beacon
#

try to use make it on main instead of on a test

#

why do you say you cant use it?

#

you totally can use a constructed type inside your zig code

lilac flume
#

If I just add this code:

var t: T = T {
        .FLAG_FULLSCREEN_MODE = false,
        .FLAG_WINDOW_RESIZABLE = false,
        .FLAG_WINDOW_UNDECORATED = false,
        .FLAG_WINDOW_TRANSPARENT = false,
    };

    std.debug.print("t = {}\n", .{t});
#

the test just dont do anything

#

its not even trying to compile

raven beacon
#

T is not on global scope

lilac flume
#

sure, I mean on the test itself

#

just after I have the T created

raven beacon
#

btw

#

why do you want to convert an enum to a struct?

lilac flume
#

So I can use it as flags

#

I wish enum would support Enum.A | Enum.B or something like this

raven beacon
#

I am not sure of why you cant with the enum

#

doesnt?

#

it has a way

#

hold on

lilac flume
#

I can convert it to an int

#

but I want to be explicit

#

this is the same behaviour when using main

meager surge
#

you're setting is_comptime to true, but setting default_value to null

#

this should issue an error message, but basically that's not allowed

#

a comptime field must always have a default value

#

in this case, it seems like you don't want a comptime field

#

so just set that to false, and I intuit it ought to work

lilac flume
#

Thank you 🙂
I false it actually what I what I believe.

meager surge
#

btw you could also just do const en: bt.Type.Enum = @typeInfo(EnumFlags).Enum;