Deprecate EnumList
Sleitnick opened this issue · 2 comments
EnumList existed before Luau types. Now that we can create singleton types, we can construct a union of strings to represent an enum.
--!strict
-- Create enum using a union of strings:
type MyEnum = "A" | "B" | "C"
-- Roblox will type-check this to be either A, B, or C, and nothing else:
local myEnum: MyEnum = "A"
The benefits of this:
- Proper intellisense/linting
- Serialization (e.g. Can send across network; can save in DataStore)
- Easier to debug
- No hidden behavior (they're just strings)
What is your opinion on using a simplified dictionary as an enum, such as
local MyEnum = {
A = 1,
B = 2,
C = 3
}
local myEnum = MyEnum.A
though for this, I'm not sure how the typing would work
@nicholasforeman You could certainly do something like that, but AFAIK there's no way in Luau to indicate that a type should be a key of a given struct. E.g. you can't create a type that represents either A, B, C in MyEnum
. Without being able to narrow the type, it becomes a bit difficult. e.g. if you want to create a function that takes a field of MyEnum, there's no way to represent that currently.
This is easy to do in TypeScript, but not possible (yet) in Luau.