mirror of
https://github.com/apple/pkl.git
synced 2026-01-11 22:30:54 +01:00
Infinite loop with same name #112
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @jw-y on GitHub (Mar 14, 2024).
tmp.pkl
Eval doesn't finish while
tmp2.pkl
Here eval outputs correctly.
I get that
foosin rhs offoos=foosis referring to it it's own,Bar'sfoos,therefore the infinite loop, but I think this is counter-intuitive
or is it duplicate of #117
@StefMa commented on GitHub (Mar 14, 2024):
I guess it is "intuitive" 🤔
You create a new object by extending
Bar.In your new object you set the variable to the exact same variable. => Recursive.
If you want to use the one from your parent class, you can use
super.foos.And this works 🙃
See https://pkl-playground.vercel.app/?share=officer-bush-group
@bioball commented on GitHub (Mar 14, 2024):
This is expected behavior (you can't define a property in terms of itself).
In order to reference the outer
foo, you'll need to qualify that reference somehow so that Pkl knows that you're referring to a different foo.In your case, you can either use
module.foo, orouter.foo, becausefoois defined on the module and it is one lexical scope higher.In the case where it's not available off
module, it's common to do something like this:@jw-y commented on GitHub (Mar 15, 2024):
Thanks for the replies!
I was actually going for builder pattern in the following way
In most programming languages (if not all), you would expect
fooon the rhs to refer tofunction Bird's argument.This is "counter-intuitive" in terms of other programming languages.
I guess this boils down to pkl's two traits
thisorselfto refer to it's own memberI guess this is a quirk of pkl?
@bioball commented on GitHub (Mar 15, 2024):
It's actually not due to implicit
this; it's becausefoois in the lexical scope.Other languages that have similar scoping rules have similar quirks. For example, the following java is also defining variable
namein terms of itself, instead of assigning to the outername:To fix, you need to qualify which
nameyou're talking about:Also, BTW: the builder pattern generally isn't that useful in Pkl, because Pkl objects already fulfill the role of a "builder".
Consider:
Both
p1andp2are the same thing, butp1is more readable, and it's also clearer what31is in the first example because it's named.@bioball commented on GitHub (Mar 15, 2024):
Closing this as an issue, but feel free to keep commenting if you want to learn more about scoping or coding style!
@jw-y commented on GitHub (Mar 17, 2024):
Got it! Thanks for the detailed reply!