Functions inside comptime enum

I want to create enums at comptime, using the @Enum keyword, which have behavior. Specifically jsonParse and jsonStringify. Is there a way to define a function inside the namespace of a generated type?

You cannot add declarations when creating types. One of the motivations for removing the @Type builtin was to disallow people from doing this more clearly.

I suggest generating the enum into a file and putting your declarations there.

3 Likes

Alternatively you could put a wrapper around the enum, so basically use a struct with the enum as single field:

pub fn JSON(comptime Value: type) type {
    return struct {
        value: Value,
        // ... your methods here
    }
}
4 Likes