cue-lang / docs-and-content

A place to discuss, plan, and track documentation on cuelang.org
6 stars 1 forks source link

docs/howto: Conditionally including multiple elements in a list #117

Closed jpluscplusm closed 2 weeks ago

jpluscplusm commented 7 months ago

From Slack: https://cuelang.slack.com/archives/CLT4FD7KP/p1712225294101449

We want to include multiple items in a list conditionally.

This works if multiple if blocks are used:

x: true
y: [
    1,
    2,
    if x {3},
    if x {4},
]

... but doesn't work if we try to emit multiple values from the same if clause:

// this CUE fails to evaluate
x: true
y: [
    1,
    2,
    if x {
        3,
        4,
    }
]

@mvdan pointed out an approach which permits just a single if block to be used:

x: true
y: [
    1,
    2,
    if x for e in [
        3,
        4,
    ] {e},
]