You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Recently I've been doing generation of consistent diagrams (along with cue cmd :)) and looked for more ergonomic way to use objects than #Block & {label: "Hello"}. After some inspection I was able to run the code as pasted below.
I'm thrilled it works, but I have no idea WHY it works. As in comments, it looks like string has attributes which is completely 🤯. #ball schema is no coincidence, because #ballBase couldn't fetch default values from environment, so #ball provides defaults.
If there is something I missed please point me toward the discussion but if that's a bug I'd rather not have it fixed 😉
// #ballBase is a string with attributes?#ballBase: this = "I'm \(this.#description) ball"// #Ball looks like a schema for a struct#ball: this = {
#description: _|this.#description|*"plain"#color: this.#color// Just checking conditional interpolationif (#color!=_|_) {
#description: "a \(#color)"
}
#ballBase
}
// Concrete structsred: {
#color: "red"#ball
}
blue: {
{
#color: "blue"#ball
}
}
just: {
#description: "just a"#ball
}
// Struct below won't work. Error:// plain.#description: invalid interpolation:// ./example.cue:2:19// ./example.cue:11:19// plain: {// #ball// }
The interpolation works because this = "string" is a label binding pattern that binds the entire struct value to the string expression. When #ballBase: this = "I'm \(this.#description) ball" is embedded into a struct like red: {#color: "red"; #ball}, the this reference binds to the embedding struct context (the red struct), allowing \(this.#description) to access fields from that context. The evaluation order is: (1) #ball definition is processed with its fields, (2) #ballBase is embedded, (3) this.#description resolves from the parent struct's fields (set by conditionals in #ball), (4) string interpolation completes, (5) the entire struct unifies to become that string value. The pattern fails for plain: {#ball} because #description defaults to requiring this.#description but without #color defined, the conditional doesn't set it, causing an "invalid interpolation" error since the reference is unresolvable. Fix: Add an explicit default field value or define #color to satisfy the interpolation dependency (see #ball definition at line 6-7 where #description: _ | this.#description | *"plain" requires the field to exist).
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Recently I've been doing generation of consistent diagrams (along with
cue cmd:)) and looked for more ergonomic way to use objects than#Block & {label: "Hello"}. After some inspection I was able to run the code as pasted below.I'm thrilled it works, but I have no idea WHY it works. As in comments, it looks like string has attributes which is completely 🤯.
#ballschema is no coincidence, because#ballBasecouldn't fetch default values from environment, so#ballprovides defaults.If there is something I missed please point me toward the discussion but if that's a bug I'd rather not have it fixed 😉
Beta Was this translation helpful? Give feedback.
All reactions