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

Implement tagged union types in core (and eventually GDScript) based on std::variant #8265

Open
nlupugla opened this issue Oct 26, 2023 · 3 comments

Comments

@nlupugla
Copy link

Describe the project you are working on

Godot engine.

Describe the problem or limitation you are having in your project

The Godot engine does not have a type-safe way to represent a value that could be one of several specified types. For example, there is no way to declare that a return value will be a meaningful answer OR an error code. Instead, you must return the value AND an error code, which makes for both less efficient and less readable code. Another example: there is no way to declare that a value will represent a type by being either a Variant::Type, a class name StringName, OR a script Ref<Script>. These limitations affect both core development and user scripting.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

A tagged-union is a way to represent values which may be one of several types. Introducing tagged unions to GDScript already has a ton of support (see #737). Having tagged unions in core would not only be a first step towards making that a reality, it would also improve the core development experience.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

I propose introducing a union type to core that functions in much the same way as C++17's std::variant. If std::variant cannot be used directly because of the drawbacks associated with using the standard library, I propose rolling our own based on the many open-source implementations of std::variant (or perhaps boost's variant https://www.boost.org/doc/libs/1_83_0/doc/html/variant.html or variant2 https://www.boost.org/doc/libs/develop/libs/variant2/doc/html/variant2.html).

One tricky aspect is figuring out how this implementation of std::variant would integrate with Godot's existing Variant type. One route would be to reimplement Godot's Variant as a special case of the more general std::variant implementation, but this approach is probably way too much work. I'm open to ideas for this particular challenge.

If this enhancement will not be used often, can it be worked around with a few lines of script?

No.

Is there a reason why this should be core and not an add-on in the asset library?

N/A.

@Calinou
Copy link
Member

Calinou commented Oct 26, 2023

Instead, you must return the value AND an error code, which makes for both less efficient and less readable code.

For this particular use case (which I expect to be far more common overall), a dedicated Option/Result type is likely more efficient.

@nlupugla
Copy link
Author

Perhaps. And I'd be happy with that as well. Although I suspect that implementing general union type may not be much more work than implementing a dedicated Option/Result type. I'm happy to be proven wrong though.

@ryanabx
Copy link

ryanabx commented Oct 26, 2023

This proposal should also have topic:core as it's first proposed to be a core change, then gdscript.

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

No branches or pull requests

4 participants