-
Notifications
You must be signed in to change notification settings - Fork 302
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
proposal: add downcasts #454
Comments
Original reply by @zombiezen in cuelang/cue#454 (comment) I just realized that the downcasting should probably imply unifying with its value, so my desired solution would actually look like:
|
Original reply by @mpvl in cuelang/cue#454 (comment)
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. |
Original reply by @mpvl in cuelang/cue#454 (comment) 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 |
Original reply by @proppy in cuelang/cue#454 (comment) 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:
|
Original reply by @mpvl in cuelang/cue#454 (comment) @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 desire came up again in discussion in the "language" channel of the "CUE" Slack team. |
As this is probably not a hugely common operation to perform, I wonder whether a builtin function might be more appropriate than using an operator. Given that this is essentially cutting out everything from one operand that's not in the other, I wonder if a nice name might be just For example:
Note: I suspect we don't want entirely regular unification between the two operands, because we probably want to specifically allow fields that aren't mentioned in the closed struct. For example, One useful invariant to consider: |
I assume you mean I'm not clear that the first proposed argument to So your example should (to my mind) work with |
In part to clarify my own understanding: It seems like downcasting would be a very special case of "Querying" in #165, right? Like #Cut: {
fit: {} // i.e. the struct to "fit" to
from: {} // the struct to cut from
fields: [for k,v in fit { k }]
out: from.[@ in fields] // not sure about the syntax here?
} so |
@nyarly FWIW I'm seeing it as more comprehensive than that - specifically it would act recursively, which would make it different from the model you just suggested. Also, that model doesn't cater for pattern constraints, because the |
@rogpeppe That's an important distinction! You can see my initial grasp of the problem in #165 - I've been laying a lot of hopes at the doorstep of Query that may not be founded. If there's to be a (which leads to the very odd case, I think, of intentionally including |
Adding notes from a recent conversation with @rogpeppe that there might well be merit in a first cut of some downcast mechanism only working on concrete values. For two reasons:
In this regard I note my comment in #943 (comment), specifically that a downcast operator could be spelt as follows: #largeDef: _ // a large definition
d: #largeDef
#smallDef: _ // a small definition that subsumes #largeDef (ignoring closedness)
v: concrete(d, #smallDef) |
Originally opened by @zombiezen in cuelang/cue#454
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: