Skip to content

Instantly share code, notes, and snippets.

@wowotek
Created February 13, 2025 04:08
Show Gist options
  • Save wowotek/de23a0250b17b2384dd70c4a29bfd7c4 to your computer and use it in GitHub Desktop.
Save wowotek/de23a0250b17b2384dd70c4a29bfd7c4 to your computer and use it in GitHub Desktop.
const std = @import("std");
const eEnumUntyped = enum {
val1,
val2,
val3,
val4
};
const eEnumTyped = enum (u32) {
val1,
val2,
val3,
val4
};
const sObject = struct {
prop_untyped: eEnumUntyped,
prop_typed: eEnumTyped
};
pub fn main() !void {
var objects: [2]sObject = undefined;
objects[0] = sObject{
.prop_untyped = eEnumUntyped.val3,
.prop_typed = eEnumTyped.val2
};
std.debug.print("object 0 : {} {} \n", .{objects[0].prop_untyped, objects[0].prop_typed});
std.debug.print("object 1 : {} {} \n", .{objects[1].prop_untyped, objects[1].prop_typed});
}
@thecodingend
Copy link

updated version with @intFromEnum, current version panics, thank you for sharing this!

const std = @import("std");

const eEnumUntyped = enum { val1, val2, val3, val4 };

const eEnumTyped = enum(u32) { val1, val2, val3, val4 };

const sObject = struct { prop_untyped: eEnumUntyped, prop_typed: eEnumTyped };

pub fn main() !void {
    var objects: [2]sObject = undefined;

    objects[0] = sObject{ .prop_untyped = eEnumUntyped.val3, .prop_typed = eEnumTyped.val2 };

    std.debug.print("object 0 : {x} {x} \n", .{ @intFromEnum(objects[0].prop_untyped), @intFromEnum(objects[0].prop_typed) });
    std.debug.print("object 1 : {x} {x} \n", .{ @intFromEnum(objects[1].prop_untyped), @intFromEnum(objects[1].prop_typed) });
}

@wowotek
Copy link
Author

wowotek commented Apr 2, 2025

@thecodingend I forgot i posted this question, i also forget where i post the context for this question. ahhahahaha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment