apple / apple/pkl

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

Open
#966 4 comments 8 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
11.5k
Forks
402
Avg merge
1d 15h
Merged PRs (30d)
20

Description

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)
}
```

Contributor guide

Open the contributing guide

Research direction

The issue names no files, tests, or entry points. Start by locating the implementation of imports and existing typealiases, then compare their behavior with the proposed deconstruction and alias forms. Done means the requested syntax and its interactions are defined and covered for the examples and invalid cases.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.