-
Notifications
You must be signed in to change notification settings - Fork 259
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
Local variables inside for loops yield an error #549
Comments
This is a short-coming we have at the moment. If, as in this case, you only use it in one expression, you can use
If you do need it in multiple places, a (admittedly sub-optimal) workaround is to use a
|
My case was initially trying to group objects and map each subgroup under different properties of the result, but don't add the result property if the corresponding subgroup is empty. And another loop on top of that. It went like this Attempt 1: local variable in for-loopclass Entity {
selectors: Listing<Selector>
}
class Selector {
enabled: Boolean
index: Int
}
hidden objects = new Listing<Entity> {
new {
selectors {
new {
enabled = false
index = 1
}
new {
enabled = true
index = 2
}
}
}
new {
selectors {
new {
enabled = false
index = 3
}
new {
enabled = true
index = 4
}
}
}
}
result: Listing = new {
for (o in objects) {
local grouped = o.selectors.toList().groupBy((x) -> x.enabled)
new {
when (grouped.containsKey(true)) {
all_enabled {
for (a in grouped[true]) {
a.index
}
}
}
when (grouped.containsKey(false)) {
all_disabled {
for (a in grouped[false]) {
a.index
}
}
}
}
}
} Error:
Attempt 2: local variable inside leaf objectresult: Listing = new {
for (o in objects) {
-
new {
+ local grouped = o.selectors.toList().groupBy((x) -> x.enabled)
when (grouped.containsKey(true)) { Error:
Attempt 3: instance methodsI eventually was able to solve the problem with some instance methods on Entity. However both errors above were rather unexpected for me. Curiously, removing |
I actually tried that as well! Doesn't work either, apparently because |
Works for me with that
but also... this pattern of splitting a collection in two sub-collections based on a property is commonly called partitioning. Pkl has a
(I'm omitting the |
Should've tried that! I've put it inside
That's an example. Does it matter?
That's nice! Thanks for the hint. To summarize what I felt was inconvenient:
Hope will be addressed in the future versions! |
Code:
On evaluation yields the following error:
Instead, expected to produce object with entries
2 3 4
.Version:
The text was updated successfully, but these errors were encountered: