# Multi-valued enum

**URL:** <https://forum.crystal-lang.org/t/multi-valued-enum/9140>\
**Category:** Crystal Contrib\
**Created:** [September 10, 2026, 8:59am UTC](https://forum.crystal-lang.org/t/multi-valued-enum/9140 "2026-09-10T08:59:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hutou](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hutou/32/1337_2.png) [@hutou](https://forum.crystal-lang.org/u/hutou)\
**Post date:** [September 10, 2026, 8:59am UTC](https://forum.crystal-lang.org/t/multi-valued-enum/9140/1 "2026-09-10T08:59:40Z")

</div>

Speaking of AI, I once asked ChatGPT to suggest the best way to code a variable or type that would meet the following conditions: 1) never occur, 2) occur once, 3) repeat times?  
It suggested the following code:

```auto
struct Occurrence(T)
  enum Type
    Once
    Never
    Repeatable(Int32)
  end

  property value : T
  property type : Type

  def initialize(@value : T, @type : Type)
    raise "Invalid type" if @type == Type::Repeatable && @type.arg <= 0
  end

  def generate
    case @type
    when Type::Once
      [@value]
    when Type::Never
      []
    when Type::Repeatable
      Array.new(@type.arg, @value)
    end
  end
end

# Examples of use
once = Occurrence.new("Unique", Occurrence::Type::Once)
never = Occurrence.new("Never happens", Occurrence::Type::Never)
three_times = Occurrence.new("Three times", Occurrence::Type::Repeatable(3))

puts once.generate # Output: ["Unique"]
puts never.generate # Output: []
puts three_times.generate # Output: ["Three times", "Three times", "Three times"]

```

At the time, I think it was version 3.5 of ChatGPT—far from being as powerful as it is today—and the suggested syntax was, of course, incorrect.  
Nevertheless, even though the `enum` here wasn’t directly similar to the concept of an “open enum” we’re discussing, I found the approach of making each item multi-valued to be interesting.  
I didn’t look into it any further, though.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [September 10, 2026, 10:01am UTC](https://forum.crystal-lang.org/t/multi-valued-enum/9140/2 "2026-09-10T10:01:08Z")

</div>

This concept is a tagged union. Rust for example allows this.

It’s actually not that far from enums in Crystal which can also be considered a tagged union of unit types.The extra step is just that the types can have more than a single value.

An equivalent in Crystal would be an actual union type:

```auto
record Once
record Never
record Repeatable(T), value : T

Type = Once | Never | Repeatable(Int32)

```
