#how does comptime behave now?

1 messages · Page 1 of 1 (latest)

hushed stream
#

hello

if i remember correctly comptime stuff changed not so long ago.

pub fn getAttributeFromType(
    comptime T: type
) Attribute {
    comptime switch(T) {
        i8 => return Attribute.init(.i8, .x, false),
        u8 => return Attribute.init(.u8, .x, false),
        i16 => return Attribute.init(.i16, .x, false),
        u16 => return Attribute.init(.u16, .x, false),
        i32 => return Attribute.init(.i32, .x, false),
        u32 => return Attribute.init(.u32, .x, false),
        f16 => return Attribute.init(.f16, .x, false),
        f32 => return Attribute.init(.f32, .x, false),
        f64 => return Attribute.init(.f64, .x, false),
        else => {
            if( comptime @hasDecl(T, "is_xy") ) {
                var attr = getAttributeFromType(T.Element);
                attr.size = .xy;
                return attr;
            }

            @compileError("Given type '" ++ @typeName(T) ++ "' isnt supported.");
        }
    }
}```

so in this example i dont even know if either comptime (in the code, not for the argument) are needed at all. since the argument is comptime, isnt the whole function comptime? 

thanks
#

well ZLS just told me the comptime in the if condition is redundant so ig that like that the whole function is comptime now? 🤔

high raft
#

yes, the result of the entire function can be determined at comptime so it is

hallow dragon
#

small nitpick: could do

comptime switch (T) {
    i8, u8, i16, u16, i32, u32, f16, f32, f64 => |Num| {
        return Attribute.init(@field(Enum, @typeName(Num)), .x, false);
    },
    else => {
        // -- snip --
    },
};
hushed stream
#

🤔

#

that's way more compact, thanks :D

hallow dragon
# hushed stream that's way more compact, thanks :D

I would probably do that kind of stuff with type reflection:

comptime {
    switch (@typeInfo(T)) {
        .Int, .Float => switch (@bitSizeOf(T)) {
            8, 16, 32, 64 => return Attribute.init(@field(Attribute.Kind, @typeName(T)), .x, false),
            else => {},
        },
        .Array => |arr| blk: {
            const s = switch (arr.len) {
                2 => .xy,
                3 => .xyz,
                4 => .xyzw,
                else => break :blk,
            };
            return Attribute.init(@field(Attribute.Kind, @typeName(T)), s, false);
        },
        .Struct, .Enum, .Union, .Opaque => if (@hasDecl(T, "is_xy")) {
            var attr = getAttributeFromType(T.Element);
            attr.size = .xy;
            return attr;
        },
        else => {},
    }
    @compileError("Given type '" ++ @typeName(T) ++ "' isn't supported");
}
#

though either way works

#

type reflection is slightly longer, though less repetiteve

hushed stream
#

i ended up with this

hallow dragon
#

fair

hushed stream
#

how about that?

#

idk if inline .Array, .Vector is valid tho

hallow dragon
#

if it compiles it works

hushed stream
#

fair point giggle

hallow dragon
#

if it doesn't, that's probably a bug

hushed stream
#

it is suppose to work tho?

hallow dragon
#

btw, why split up Int and Float

hushed stream
#

not the same widths supported

hallow dragon
#

I see

#

could still combine 'em

hushed stream
#

idk how without having to use a label and break

hallow dragon
#
.Int, .Float => switch (@bitSizeOf(T)) {
    8 => if (@typeInfo(T) == .Int) return Attribute.init(...),
    16, 32 => return Attribute.init(...),
    64 => if (@typeInfo(T) == .Float) return Attribute.init(...),
},
hushed stream
#

it think for me it's more readable in two switch branches rather than that