Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate EnumList #143

Open
Sleitnick opened this issue Apr 24, 2023 · 2 comments
Open

Deprecate EnumList #143

Sleitnick opened this issue Apr 24, 2023 · 2 comments

Comments

@Sleitnick
Copy link
Owner

Sleitnick commented Apr 24, 2023

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)
@nicholasforeman
Copy link

nicholasforeman commented Apr 24, 2023

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

@Sleitnick
Copy link
Owner Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants