mirror of
https://github.com/apple/pkl.git
synced 2026-01-11 22:30:54 +01:00
Functions are not inherited by amending modules #103
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 @philippemerle on GitHub (Mar 4, 2024).
The language reference says that a module amending another module inherits from all the members of the amended module. But functions of the amended module seem to be not inherited from as shown in the following example.
File
base_template.pklFile
my_template.pklEvaluating
my_template.pklproduces the following errorDo I miss something?
@stackoverflow commented on GitHub (Mar 4, 2024):
Scoping rules in Pkl can be a bit tricky. You can use
moduleto reference top level variables and functions:Also: if you use our IntelliJ plugin you should see an error telling you this function is not found.
@philippemerle commented on GitHub (Mar 4, 2024):
Thank you the provided solution. But this will be simpler if inherited functions could be called directly without prefixing them by
module.. Perhaps in a future pkl release?@bioball commented on GitHub (Mar 4, 2024):
The scoping rules in place here provide an important safety net; that the resolved member can't change by adding something else in the (grand)parent module.
Without this rule, it would be possible to change what a variable/method resolved to by changing something in the base module. It would make it so that adding names to a module might produce very unexpected results in the generated configuration.
For example, if this worked:
parent.pkl
child.pkl
Then, a new
function foo()was introduced:This change seems innocent when just looking at
parent.pkl, but it would changefoo()withinchild.pklto resolve to a different method. This behavior would make it too easy for pkl code to break in hidden ways.The same resolution rules apply to properties, too.
@bioball commented on GitHub (Mar 4, 2024):
Closing as not planned, as this works as-intended.