# Comptime Enum Value Declarations

**URL:** <https://ziggit.dev/t/comptime-enum-value-declarations/6123>\
**Category:** Help\
**Tags:** language\
**Created:** [September 24, 2024, 8:38pm UTC](https://ziggit.dev/t/comptime-enum-value-declarations/6123 "2024-09-24T20:38:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![haydenridd](https://ziggit.dev/user_avatar/ziggit.dev/haydenridd/32/2375_2.png) [@haydenridd](https://ziggit.dev/u/haydenridd)\
**Post date:** [September 24, 2024, 8:38pm UTC](https://ziggit.dev/t/comptime-enum-value-declarations/6123/1 "2024-09-24T20:38:57Z")

</div>

I’m pretty sure I already know the answer, but wanted to throw this out there as a last ditch. Let’s say I have an enum that can have different fields depending on some compile time known configuration. A working example of how I could do this is as follows:

```Zig
const std = @import("std");

const comptime_option = true;

const SomeEnum = if (comptime_option) enum(u32) {
    a = 0,
    b,
    c,
    d,

    pub fn someMethod(self: SomeEnum) u32 {
        return @intFromEnum(self);
    }
} else enum(u32) {
    // Different ints for each value!
    a = 0,
    c,
    d,
    b,
    // More values!
    x,
    z,

    pub fn someMethod(self: SomeEnum) u32 {
        return @intFromEnum(self);
    }
};

pub fn main() void {
    std.debug.print("Works: {d}\n", .{SomeEnum.a.someMethod()});
    std.debug.print("Compile Error!: {d}\n", .{SomeEnum.x.someMethod()});
}

```

This accomplishes what I’m after, however you’ll notice the repeated code in both arms of the enum definition:

```Zig
    pub fn someMethod(self: SomeEnum) u32 {
        return @intFromEnum(self);
    }

```

Any way to avoid this repeated method definition or is this sorta just the way it is?

---

<div class="post-metadata">

**Author:** ![haydenridd](https://ziggit.dev/user_avatar/ziggit.dev/haydenridd/32/2375_2.png) [@haydenridd](https://ziggit.dev/u/haydenridd)\
**Post date:** [September 24, 2024, 8:48pm UTC](https://ziggit.dev/t/comptime-enum-value-declarations/6123/2 "2024-09-24T20:48:41Z")

</div>

I’ll wait for other answers to roll in, but [this](https://ziggit.dev/t/mixin-alternative-using-const-declarations/6091/2) post from @AndrewCodeDev coincidentally sort of answered my question! Here is a decent way to avoid code duplication when you have many methods that might be rather long:

```Zig
const std = @import("std");

const comptime_option = true;

const SomeEnumImpl = struct {
    pub fn someMethod(self: SomeEnum) u32 {
        return @intFromEnum(self);
    }
    pub fn otherMethod(self: SomeEnum) u32 {
        return @intFromEnum(self) * 2;
    }
};

const SomeEnum = if (comptime_option) enum(u32) {
    a = 0,
    b,
    c,
    d,

    pub const someMethod = SomeEnumImpl.someMethod;
    pub const otherMethod = SomeEnumImpl.otherMethod;
} else enum(u32) {
    // Different ints for each value!
    a = 0,
    c,
    d,
    b,
    // More values!
    x,
    z,

    pub const someMethod = SomeEnumImpl.someMethod;
    pub const otherMethod = SomeEnumImpl.otherMethod;
};

pub fn main() void {
    std.debug.print("Works: {d}\n", .{SomeEnum.b.someMethod()});
    std.debug.print("Compile Error!: {d}\n", .{SomeEnum.x.someMethod()});
}

```

You duplicate code by defining the public `const` members for each method, but is a lot more compact than duplicating the entire method code.

---

<div class="post-metadata">

**Author:** ![chung-leong](https://ziggit.dev/user_avatar/ziggit.dev/chung-leong/32/999_2.png) [@chung-leong](https://ziggit.dev/u/chung-leong)\
**Post date:** [September 24, 2024, 9:59pm UTC](https://ziggit.dev/t/comptime-enum-value-declarations/6123/3 "2024-09-24T21:59:40Z")

</div>

At the moment we don’t have a way to attach decls to a type created though `@Type()`, so the best you can do is avoiding redeclaration of methods using Andrew’s method.

---

<div class="post-metadata">

**Author:** ![mnemnion](https://ziggit.dev/user_avatar/ziggit.dev/mnemnion/32/2478_2.png) [@mnemnion](https://ziggit.dev/u/mnemnion)\
**Post date:** [September 25, 2024, 12:07am UTC](https://ziggit.dev/t/comptime-enum-value-declarations/6123/4 "2024-09-25T00:07:37Z")

</div>

The eloquent way to get this is `usingnamespace`.

```zig
fn EnumImpls(E: type) type {
    return struct {
         pub fn methodA(self: E, ...) void {
              // ... 
         }
         // etc
    };
}

const SomeEnum = if (comptime_thing) enum(u32) {
   fee,
   fie, 
   foe,

   pub usingnamespace EnumImpls(@This());
} else enum(u32) {
    other,
    tags,
    here,
     
    pub usingnamespace EnumImpls(@This());
};

```

Single source of truth.

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [September 25, 2024, 12:37am UTC](https://ziggit.dev/t/comptime-enum-value-declarations/6123/5 "2024-09-25T00:37:02Z")

</div>

The more I look into it, the more I am torn on `usingnamespace` - have we opened a thread dedicated to this topic yet? I think we should if we haven’t.

---

<div class="post-metadata">

**Author:** ![mnemnion](https://ziggit.dev/user_avatar/ziggit.dev/mnemnion/32/2478_2.png) [@mnemnion](https://ziggit.dev/u/mnemnion)\
**Post date:** [September 25, 2024, 1:13am UTC](https://ziggit.dev/t/comptime-enum-value-declarations/6123/6 "2024-09-25T01:13:09Z")

</div>

I don’t think so, and that might be a good idea. The [issue on the tracker](https://github.com/ziglang/zig/issues/20663) has seen a lot of action, a discussion here would end up fairly high signal, that’s likely to be a net positive contribution.

I’ll encourage you to start a brainstorm when you find time, you would do a good job of being evenhanded about the pros and cons. I’m decidedly pro, wouldn’t want to put my thumb on the scale as the first post on the topic.
