apple / apple/pkl

Support separating typealiases into separate files without stuttering

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

Description

Pkl uses type aliases to define enums and certain custom types:

```pkl
typealias MyEnum = "apple" | "banana"

typealias SemVer = String(matches(Regex(#"^\d+\.\d+\.\d+$"#)))
```

However, if I put these in separate files, I get stuttering usages:

```pkl
module Config

import "MyEnum.pkl"
import "SemVer.pkl"

my_enum: MyEnum.MyEnum? = null

version: Semver.Semver = "1.2.3"
```

I therefore propose supporting a special type of pkl file, a type file, one containing a(n almost) lone typealias:

```pkl
module my.namespace.MyEnum

hidden apple = "Apple"
hidden banana = "Banana"

typealias MyEnum = apple | banana
```

which would allow usage without stuttering:

```pkl
module Config

import "MyEnum.pkl"

my_enum: MyEnum = "banana"
```

Another way to see it is that if the only visible variable in the file has the same name as the file itself, it's automatically exposed without stuttering.

This could of course be expanded to apply to classes and other types as well, and maybe there is a place for it - in the beginning I struggled a lot with not being able to have a module amend a class from another module etc.. However, this requires more discussion and is likely out of scope for this feature request. In this request I'm specifically asking to be allowed to put my enums/typealiases in separate files without stutter.

Contributor guide

Open the contributing guide

Research direction

Start with the typealias examples in MyEnum.pkl and SemVer.pkl and trace how their imports are resolved in Config. Compare the current stuttering names with the proposed MyEnum usage; done means a separate typealias file can be imported without the repeated file/type qualifier, while broader class behavior remains out of scope.

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
Active
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.