Use of mixin
with spread
gives unexpected result
#562
-
Hi, everyone! I'm trying to deeply merge 2 dynamic objects and found out about // Working example
hidden defaultParams {
options {
enabled = false
someOpt = "someValue"
}
}
hidden overridesLiteral: Mixin<Dynamic> = new {
options {
enabled = true
}
}
merged = defaultParams |> overridesSpread Gives expected output: merged {
options {
enabled = true
someOpt = "someValue"
}
} However, when using // Spread example
hidden defaultParams {
options {
enabled = false
someOpt = "someValue"
}
}
hidden customParams {
options {
enabled = true
}
}
hidden overridesSpread: Mixin<Dynamic> = new {
...customParams
}
merged = defaultParams |> overridesSpread Gives different output: merged {
options {
enabled = true
}
} What am I missing here and could it even be archived with use of a |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
This is an extension of the case that came up in #505: the spread operator performs a property/member-wise assignment. In contrast, applying a mixin performs a property/element-wise amend. Accordingly, in your example, the spread usage:
Is "de-sugared" to:
And here it's clearer to see that the resulting value for It's a little tricky to determine what you're going for overall here, but if I'm inferring correctly, it seems like |
Beta Was this translation helpful? Give feedback.
-
Thanks for the explanation, now I see what is going on. While reducing my problem to provided examples I completely missed the important context - I'm reading # customParams.yaml
options:
enabled: true And then the full example would be as such: import "pkl:yaml"
hidden defaultParams {
options {
enabled = false
someOpt = "someValue"
}
}
hidden customParams = new yaml.Parser {}.parse(read("file:customParams.yaml"))
hidden overridesSpread: Mixin<Dynamic> = new {
...customParams
}
merged = defaultParams |> overridesSpread merged {
options {
enabled = true
}
} I've tried
but got an error:
Is it possible to parse the file directly to |
Beta Was this translation helpful? Give feedback.
I ask, because you're defining Pkl's amending quite precisely there. It sounds like you want a
MyParams.pkl
(upper case, because a template with defaults and type definitions, meant to be amended) with something like:and in a subdirectory, somewhere, you might have
myParams.pkl
(lower case, because amending a template);Now when you
pkl eval myParams.pkl
, you get