Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Easier conditional groups in list comprehension #798

Closed
verdverm opened this issue Feb 26, 2021 · 6 comments
Closed

Easier conditional groups in list comprehension #798

verdverm opened this issue Feb 26, 2021 · 6 comments
Labels
FeatureRequest New feature or request wontfix This will not be worked on

Comments

@verdverm
Copy link
Contributor

Is your feature request related to a problem? Please describe.

I have a list which has elements that are conditionally included. Some of them are groups and always appear together. The way to write this most intuitively is an error.

Describe the solution you'd like

a: "on"

l: [
        if a == "on" {
                "a",
                "A",
        },
        "b",
        "c",
]
l.0: conflicting values "A" and "a":
    ./list.cue:5:3
    ./list.cue:6:3

Describe alternatives you've considered

  • wrapping each in the same conditional (bad UX)
  • extracting the group into its own list and putting the conditional in a nested list comp [for x in aa if a == "on" { x } ]
@verdverm verdverm added the FeatureRequest New feature or request label Feb 26, 2021
@verdverm verdverm changed the title Easier nested list comprehension Easier conditional groups in list comprehension Feb 26, 2021
@myitcv
Copy link
Contributor

myitcv commented Feb 27, 2021

This will be addressed by the query proposal in #165:

a: "on"

l: [
        if a == "on" {
                ["a", "A"].*
        },
        "b",
        "c",
]

@myitcv myitcv closed this as completed Feb 27, 2021
@verdverm
Copy link
Contributor Author

Is there anything that hard prevents the suggested syntax above? (@mpvl)

The benefit I see with it is:

  • the ease of line edits, i.e. I can add or remove a line rather than editing within a line
  • no need for extra list style syntax, the query syntax is only marginally better than the list comp syntax

@myitcv
Copy link
Contributor

myitcv commented Mar 1, 2021

Is there anything that hard prevents the suggested syntax above?

It's (currently) illegal because it's neither a valid embedded scalar, nor it is a valid struct value.

the ease of line edits, i.e. I can add or remove a line rather than editing within a line

Like this?

a: "on"

l: [
        if a == "on" {[
                "a", 
                "A"
        ].*},
        "b",
        "c",
]

no need for extra list style syntax, the query syntax is only marginally better than the list comp syntax

It's true there is a close relationship between the two, but I'm not sure we'd want to introduce a "third" way of writing things.

Re-opening for @mpvl's answer in any case.

@myitcv myitcv reopened this Mar 1, 2021
@verdverm
Copy link
Contributor Author

verdverm commented Mar 1, 2021

For some extra context, this started from a discussion on Slack, where one of the examples is the following

a: "on"
x: "on"
b: "on"
c: "off"
aa: ["a", "A", "eh?"]
xx: ["x", "x", "ex"]
l: [
        for A in aa if a == "on" { A }
        for X in xx if x == "on" { X }
        if b == "on" {
                "b"
        }
        if c == "on" {
                "C"
        }
]
  • I was surprised that commas are optional
  • I bring it up for the sequential list comps that end up flattened in l, which feels like weird syntax to me

@mpvl mpvl added the wontfix This will not be worked on label Mar 2, 2021
@mpvl
Copy link
Contributor

mpvl commented Mar 2, 2021

@verdverm: the suggested syntax would contradict what that syntax typically means. It already means something in one context and it would be very unsurprising to have it mean something else in another. There are probably also cases where the original syntax has meaning in this context.

@cueckoo
Copy link

cueckoo commented Jul 3, 2021

This issue has been migrated to cue-lang/cue#798.

For more details about CUE's migration to a new home, please see cue-lang/cue#1078.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest New feature or request wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants