mirror of
https://github.com/apple/pkl.git
synced 2026-01-13 15:13:38 +01:00
inheriting imports on amends / extends #323
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 @jjuliano on GitHub (Jul 25, 2025).
Hello,
I have a pkl template file that have many imports defined. For example, main.pkl is my template, and foo.pkl is a supporting util library, and bar.pkl is my configuration file.
The idea is that, as a user-facing configuration file, for UX reasons, bar.pkl shouldn't have 100 import lines, where the user scrolls to the bottom of the file to enter their configurations.
I assume that since the imports are already in the non user-facing pkl files, then by doing 'amends' or 'extends' the config files can just access those imports.
At the moment, I did several test for this functionality as seen below. Is there another way to do this without writing a chaining function on the template?
main.pkl
foo.pkl
bar.pkl
baz.pkl
qux.pkl
@bioball commented on GitHub (Jul 25, 2025):
You can expose the import as a property. This makes makes it a
hidden const, which means:Now, you can reference
fooplainly in the amending/extending module. If you're in a nested scope, you need to use themodulekeyword:Note: an import gives you both a value and a type. If you want to expose imported types to a child module, you can create a typealias:
@jjuliano commented on GitHub (Jul 26, 2025):
Thanks @bioball, this works, however, for the nested, is it possible to not have to call
moduleto access the function?@bioball commented on GitHub (Jul 26, 2025):
This is an intentional rule; it guarantees that adding/removing names in the base module will not change the meaning of a name lookup in an amending/extending module.
You can read more about this rationale here: https://github.com/apple/pkl/discussions/865#discussioncomment-11640312