Skip to content

Pattern for working with Enum variants #414

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

Open
alexparlett opened this issue Apr 13, 2025 · 1 comment
Open

Pattern for working with Enum variants #414

alexparlett opened this issue Apr 13, 2025 · 1 comment

Comments

@alexparlett
Copy link

alexparlett commented Apr 13, 2025

Is your feature request related to a problem? Please describe.
Handling of enums feels relatively complex especially when working with things such as key codes, having to construct a variant, or convert to variant name and then do a string comparison makes our script api relatively unfriendly to users when considering things like modding etc.

i.e. with the input being KeyboardInput event.

function on_input(event)
    local key = event.key_code:variant_name()

    if (key == "KeyA") then
        info("move left")
    end
end
local KeyA = construct(types.KeyCode, {
  variant = "KeyA"
})

function on_input(event)
    if (event.key_code == KeyA) then
        info("move left")
    end
end

Describe the solution you'd like
A simpler way to handle enum variants, or even a well defined set of globals for bevy bindings (and a way for the user to easily define their own)

function on_input(event)
    if (event.key_code == KeyCode.KeyA) then
        info("move left")
    end
end
function on_input(event)
    if (event.key_code == keys.KeyA) then
        info("move left")
    end
end

Describe alternatives you've considered
This could be defined in a lua set of globals relatively easily.

keys = {
  KeyA = construct(types.KeyCode, {
    variant = "KeyA"
  })
}

Or register a global manually.

    registry.register("keys", move |wg: WorldGuard| {
        let allocator = wg.allocator();
        let mut allocator = allocator.write();

        let mut key_cache = HashMap::<String, ScriptValue>::default();
        for (key, name) in &key_codes {
            let payload = ReflectReference::new_allocated(*key, &mut allocator);

            key_cache.insert(name.to_string(), ScriptValue::Reference(payload));
        }

        Ok(ScriptValue::Map(key_cache))
    });

However this seems like an important binding to upstream if possible.

@makspll
Copy link
Owner

makspll commented Apr 20, 2025

Definitely, iterating over all enums and generating something based on the variants is definitely doable, there are many options:

Global enum instances

  • for unit variants, simply construct these variants as globals in the enum's namespace i.e. MyEnum.MyUnitVariant
  • for for other variants, we could populate a constructor function for each variant i.e. MyEnum.SomeVariant({ foo = bar})

Global enum discriminators

  • for all variants, register a discriminator type under the enum's global namespace, i.e. MyEnum.MyVariant
    • then add a :variant() function on all enums
    • allowing: my_enum_instance:variant() == MyEnum.MyVariant

Some combination of both

We might want bits of both of these functionalities, and in fact constructors as a whole could be made slightly more readable this way, then we would need to differentiate between constructors and discriminants, perhaps using: MyEnum.variants.MyVariant

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