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