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

Union types: discriminate across table types and between enums | strings #108

Open
pdesaulniers opened this issue May 13, 2020 · 5 comments
Labels
feature request New feature or request

Comments

@pdesaulniers
Copy link
Member

As mentioned in the tutorial, the is operator can only discriminate across primitive types and at most one table type. The same limitation exists between enums and strings.

It would be nice if there wasn't such limitation.

@pdesaulniers pdesaulniers changed the title Union types: discriminate between table types and between enums | strings Union types: discriminate across table types and between enums | strings May 13, 2020
@hishamhm hishamhm added the feature request New feature or request label May 13, 2020
@hishamhm
Copy link
Member

hishamhm commented May 13, 2020

discriminate across table types

This will require metatable-equipped records (or objects (or some sort of tagged unions)). For reference, see related discussion in #97.

between enums | strings

This will require reifying the enum in the generated code for runtime lookup. Not done yet, but doable. This one seems pretty low-priority, though.

@Fang-
Copy link
Contributor

Fang- commented Sep 12, 2020

or some sort of tagged unions

Considering they're mentioned here, I'll refrain from opening a separate issue for this, but: supporting tagged unions seems very important to me. Coming from a language that supports them, they're rightfully used all over the place, and can make up for unimplemented complex union cases like the mentioned string | MyEnum.
#124 seems to be pushing in this direction already, could be worth pursuing that further.

(Regardless, excited about this project! Thanks @hishamhm and all contributors involved for the great work so far. (: )

@makspll
Copy link

makspll commented Jul 29, 2022

+1, I am embedding Teal in Rust, and the limitation of not being able to use multiple UserData types in an union is a massive problem for me, I have to basically resort to using 'Any' everywhere (mostly metamethods such as Add, Mul etc..) which then defeats the purpose of using teal in the first place :p

@lenscas
Copy link
Contributor

lenscas commented Jul 29, 2022

I think a decent compromise would be to allow table | otherTable (or userdata | otherUserData) but do not allow is to disambiguate between them.

This should allow people to create their own functions to disambiguate between the variants (which sadly would require as) and would also make documenting api's that are not written in teal easier (Like libraries written in lua but also apis written in C and Rust which might use a library to generate the documentation)

@hishamhm
Copy link
Member

hishamhm commented Jan 2, 2025

Half of this is implemented: one can now equip record types with an is handler that allows discriminating between them.

Discriminating between enums and strings is still to-do!

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

No branches or pull requests

5 participants