[Feature Request] Import deconstruction (& aliasing/macros) #280

Open
opened 2025-12-30 01:23:09 +01:00 by adam · 1 comment
Owner

Originally created by @joshqou on GitHub (Feb 19, 2025).

Deconstruction is a quality of life feature that a lot of languages have nowadays. Since Pkl lacks the ability to alias imported functions and structures, it results in a lot of repetitive ProprietaryServiceConfig.ReusedClassThing or extending a class without adding anything so it can be referenced without explicitly specifying the module. This gets quite messy when different classes result in different output and the end-user file is expected to cast as one or another.

An implementation would essentially let someone do an ES-like deconstructed import like this:

// Provides `convertToHCL` and `Terraform`
import "/path/to/file.pkl" as { convertToHCL, Terraform }
// Provides `Terraform`, and the module as `file`
import "/path/to/file.pkl" as file, { Terraform }

It would also be handy to be able to write aliases or macros within a pkl file, in a similar way to how typealiases currently work. I'm primarily interested in making it easier to repeatedly reference module exports but it could also be useful for other things as well.

import "/path/to/file.pkl"
alias convertToHCL = file.convertToHCL

output {
    value = convertToHCL(someKey)
}
Originally created by @joshqou on GitHub (Feb 19, 2025). Deconstruction is a quality of life feature that a lot of languages have nowadays. Since Pkl lacks the ability to alias imported functions and structures, it results in a lot of repetitive `ProprietaryServiceConfig.ReusedClassThing` or extending a class without adding anything so it can be referenced without explicitly specifying the module. This gets quite messy when different classes result in different output and the end-user file is expected to cast as one or another. An implementation would essentially let someone do an [ES-like](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) deconstructed import like this: ```pkl // Provides `convertToHCL` and `Terraform` import "/path/to/file.pkl" as { convertToHCL, Terraform } // Provides `Terraform`, and the module as `file` import "/path/to/file.pkl" as file, { Terraform } ``` It would also be handy to be able to write aliases or macros within a pkl file, in a similar way to how typealiases currently work. I'm primarily interested in making it easier to repeatedly reference module exports but it could also be useful for other things as well. ```pkl import "/path/to/file.pkl" alias convertToHCL = file.convertToHCL output { value = convertToHCL(someKey) } ```
Author
Owner

@bioball commented on GitHub (Feb 21, 2025):

This feature makes sense to me.

Would like some feedback from the community to help us prioritize this.

@bioball commented on GitHub (Feb 21, 2025): This feature makes sense to me. Would like some feedback from the community to help us prioritize this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#280