-
Notifications
You must be signed in to change notification settings - Fork 118
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
Enhancement: Solutions to the union issues? #785
Comments
@SlashScreen Thanks for the feedback! The good news is that the most important of these use cases (support for various Lua object systems represented as Teal records) is already addressed in the code for the next version of Teal, available in the unions and
|
Oh, I see, so an enum isn't exactly a type; any string matching the enum is that enum. Directions = {"north", "east", "south", "west"}
DirectionsSet = { -- This approach also allows for custom values in enums.
north = 0,
east = 1,
south = 2,
west = 3,
}
-- iteration
for value in Directions do
assert(not DirectionsSet[value] == nil)
end
-- is
assert(not DirectionsSet["north"] == nil)
assert(DirectionsSet["some other string"] == nil) |
An enum type is a subtype of string.
Yes, that's what I meant by "we could support [ For the sake of issue tracker housekeeping, I'm closing this issue since it is a duplicate of #108 (see also #189, #205) |
In the current version of Teal, Unions cannot have more than 1 record type, and no
string | enum
. Here, I have some solutions:For the Record problem- insert a function into the new records called
_type()
that returns a string with the record name, and use that instead oftype()
in the if-else chain generated when usingis
.For the enum problem: Instead of using a
type()
call, you can use Objects to emulate the "unique"ness of enums. Upon generation, insert a table into the generated code as a stand-in for the enum and an accompanying table set, and do checks against that. Instead ofis
usingtype()
, it can instead simply check the type based on whether that value is within the enum's Set.The remaining question: What about normal tables that aren't records? Perhaps all the record-comparing can take place inside an if statement that checks if the value has a
_type
function. To union against several types of tables, perhaps you just need to have a loop that checks the shape of the table.This I suppose introduces a bit of overhead; is this a worthy sacrifice?
The text was updated successfully, but these errors were encountered: