How to index an enum structure?

Hello,
how to index an enum structure ???
maybe it’s impossible, I can’t find an example and my thing beug for so much, it’s something simple…


pub const FUNC = enum {
    none,
    F1 ,
    F2 ,
    F3 ,
    F4 ,
    F5 ,
   pub fn....

}

var func = FUNC(1);  // FUNC.F1-->ok 
std.debug.print("got enum \"{s}\"\n", .{func.str()});  // "F1" 
....

Thank you

@enumToInt()?..

No I modified the example, maybe I was not explicit enough sorry

I’m new to Zig and as I dive into it I realize that zig doesn’t accept the unexpected because FUNC[1] or FUNC[i] can be prone to crashing

i think you’re looking for @intToEnum

test "asdf" {
    const FUNC = enum {
        none,
        F1,
        F2,
        F3,
        F4,
        F5,
    };

    var func = @intToEnum(FUNC, 1);
    std.debug.print("got enum \"{}\"\n", .{func}); // "F1"
}

output:

Test [1/1] test.asdf... got enum "main.test.asdf.FUNC.F1"
1 Like

Just to add: in case you want to index an enum’s value whose values don’t scale linearly, you can leverage std.enums.values:

const std = @import("std");

test {
    const E = enum(u8) { a = 'a', b = 'b', c = 'c' };
    const values: []const E = std.enums.values(E);
    try std.testing.expectEqual(E.a, values[0]);
    try std.testing.expectEqual(E.b, values[1]);
    try std.testing.expectEqual(E.c, values[2]);
}
1 Like

hello ,

what is the most expensive

a formula or a box

knowing that the second solution is more readable

 _ = utf.utf8Encode(c0,&vUnicode) catch unreachable;
            var vn:usize  = @intCast(u21, c0) + 50 ;
            if (vn < 51 or vn > 76 ) { Event.Key = .none; return Event;}
            else { Event.Key = @intToEnum(kbd, vn); return Event;}

or

// ctrl keys (avoids  crtl-i (x09)   ctrl-m (x0D) )
            '\x01'...'\x08','\x0A'...'\x0C','\x0E'...'\x1A' => {
                switch ( c0 + '\x60') {
                    'a' =>  { Event.Key = kbd.ctrlA; return Event;},
                    'b' =>  { Event.Key = kbd.ctrlB; return Event;},
                   ....
                    'z' =>  { Event.Key = kbd.ctrZ; return Event;},

@JPL There’s not a lot that can be said about either snippet of code without broader context. I’d advise bringing this type of “code review” question to its own post, or some other more appropriate forum.
At any rate, either one is likely to generate fairly optimal code. Switches can often be optimized into jump tables, or even LUTs (lookup tables), which are only a few instructions. In the LUT case, you may even be able to appease the CPU branch predictor by avoiding having a branch there in the first place.
Although one recommendation I might make is, if possible, to consider formulating the second one like:

Event.Key = switch (c0 + '\x60') {
    'a' => .ctrlA,
    'b' => .ctrlB,
    // ...
    'z' => .ctrlZ,
    else => .none,
};
return Event;

which would increase the chances that it generates a LUT.

I think you’re right, I leave the second solution which is very readable especially when you have to dive back into the code.
Because good code also means thinking about tomorrow.