-
Notifications
You must be signed in to change notification settings - Fork 218
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
Named Unions as function parameters #4501
Comments
I think the syntax in other languages such as Rust, Python and C# is much nicer. In Rust it would be so much cleaner: enum InvoiceStatus {
Paid,
Unpaid,
Canceled,
} |
I think this would be a very nice addition to the language! -- Do we need the mapping to ints / strings? Or could we just have the variants like (possibly the syntax would need to be slightly different — possibly |
I found the discussion of named unions in #2930 compelling, not to mention that the parsing of that syntax is already done (#4038). I would guess that we want to have just one syntax for named unions / enums, not that the existing one is perfect, but it might have certain advantages when parsing... I'm not sure, though, and I don't feel inspired to dive into the parser 😄 As for the necessity of mapping named union members to values, I think it's probably better to make it explicitly defined by users rather than implicit. I don't think there's a single convention that users would expect -- I can imagine that one user would expect Requiring explicit definitions would also help a lot if named unions (as enums) end up getting cast to other data types. This casting is probably going to be very common, especially if named unions are used for typing relations, where the underlying database types are disconnected from the types defined in the PRQL context. |
Yes, if we cast them to other types then I totally agree. |
This is a nice feature overview of exactly how I think PRQL should approach sum types! We will need to invest some time into how this will be implemented in SQL and unfortunately I won't we able to help here for some time, as I have other priorities right now. |
What's up?
From the discussion on #4499, we propose allowing functions to take parameters that are typed by named unions (aka enums, #4038).
The core syntax would look like this:
If the type of the function parameter can be resolved, it should be possible to drop the explicit reference to the type name when specifying the parameter value.
This would be handled by prepending the parameter type to the name resolution order, so
InvoiceStatus
would be searched first, followed then by the remaining namespace scopes.The same would also apply for named parameters:
With this, the
join
transformation in the standard library would have the updated definition:A similar update would also be made for
from_text format:...
As an aside, it would also be worth considering a similar feature of named union namespace resolution for typed relations, when those are implemented. I could see something like this working very well:
The text was updated successfully, but these errors were encountered: