-
Notifications
You must be signed in to change notification settings - Fork 171
Add downcasts #454
Comments
I just realized that the downcasting should probably imply unifying with its value, so my desired solution would actually look like:
|
Yes, absolutely, it would imply unification. The original spec had a definition of downcast that was exactly this (syntax + semantics). But as I couldn't find a good use case, it was dropped (for now). There have been several uses for it since, though, including this one. For instance, the downcast operator also indicates the difference between a protobuf converted to an open definition (proto over the wire) or a compiled closed definition (defining a proto value in Go). The syntax may not work, though. There is some thought of using the function syntax for macro-like substitution, a syntactic sugar that lets structs behave as functions. This does not seem to be compatible with casting. An alternative syntax could be
or
The latter would overload the The cast and "macro" operator are related, though, with a bit of squinting. So maybe it is possible to have a single solution. The "macro" operator would translate
for instance, where
If a downcast operator that could be used to achieve similar convenience would make a macro operator redundant, though. |
Note an objection against the To fix this, there should be a rule that says that fields defined within the struct should either be defined within |
I wonder if having a binary operators like Python has for sets:
@jlongtine pointed me to a recent discussion https://cuelang.slack.com/archives/CLT3ULF6C/p1599911085226700?thread_ts=1599910691.226200&cid=CLT3ULF6C which discuss a similar It could user to keep the same mental model they have about unification (and other binary operator):
While allowing easier chaining:
|
@proppy : I think having full set logic is entering scary territory. Perhaps in a struct package. @zombiezen : a problem with the One thought I had though: the plan is to expand the selection operator to allow more types to both facility the query mechanism and label mechanism:
then the idea was to allow the following corresponding RHS selectors
These could be used wherever comprehensions are used now (they create streams). Not that this makes the It is possibly problematic, but one idea would be to overload Coincidentally, it is also very close in syntax to Go's dynamic cast operator. Anyway, just brainstorming here. I'm not sure if this actually would make sense logically. But it seems more feasible than For completeness of the whole query extension direction: the idea of the pattern selection operator would be allowed to be of the form |
This issue has been migrated to cue-lang/cue#454. For more details about CUE's migration to a new home, please see cue-lang/cue#1078. |
Is your feature request related to a problem? Please describe.
I have a configuration where I have a struct containing a mapping that is frequently edited. Different declarations across my mapping require different subsets of that mapping inside their struct, but frequently not the whole thing.
In this same configuration, I sometimes use template-like objects that have definitions and I want to convert them to a more basic type without the definitions. For example:
In both cases, I have to write a rather verbose and hard-to-understand comprehension to get the desired effect:
Describe the solution you'd like
I'd like a kind of "downcasting" conversion that removes any fields in a struct that aren't in another struct. I don't have an informed opinion about syntax, but borrowing from Go's type conversion syntax, something like:
Describe alternatives you've considered
As mentioned above, this is possible in the language already using comprehensions, but is not obvious to those who haven't already dived deep into CUE what this is doing. A lack of user-defined functions makes it difficult to abstract the operation, thus bringing me to ask for language support. I could do something like (untested):
But this doesn't add terribly much clarity IMO, it just adds indirection.
The text was updated successfully, but these errors were encountered: