# 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:** 1\
**Showing post:** 2

<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.

---

_[View the full topic](https://ziggit.dev/t/comptime-enum-value-declarations/6123)._
