apple / apple/pkl

Using type alias causes error where directly using the type doesn't

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

Description

It is surprising (at least to me) that in the following pkl definition:

```pkl
typealias Step = GetStep | PutStep

class Job {
steps: Listing?
}

class GetStep {
prop1: Step?
}

class PutStep {
prop1: Step?
}
```

An error is thrown for prop1 on the PutStep saying:
```
–– Pkl Error ––
Type alias definitions must not be cyclic.

8 | prop1: Step?
^^^^
at foo#GetStep (file:///Users/jonathan.harden/test/pkl/foo.pkl, line 8)

1 | typealias Step = GetStep | PutStep
^^^^^^^
at foo#Step (file:///Users/jonathan.harden/test/pkl/foo.pkl, line 1)

4 | steps: Listing?
^^^^
at foo#Job (file:///Users/jonathan.harden/test/pkl/foo.pkl, line 4)

106 | text = renderer.renderDocument(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (https://github.com/apple/pkl/blob/0.25.2/stdlib/base.pkl#L106)
```

However if instead of the typealias, I use the value of the typealias directly it is fine:

```pkl
class Job {
steps: Listing?
}

class GetStep {
prop1: (GetStep | PutStep)?
}

class PutStep {
prop1: (GetStep | PutStep)?
}
```

It seems I should be able to use a typealias anywhere I would use a type and get the same result.

I can't see in the documentation for type alias where this is mentioned.

Clearly the workaround is to not use a type alias and instead just redefine the complex type everywhere which is ok for now, but it's a surprising limitation.

Versions:

```
$ pkl --version
Pkl 0.25.2 (macOS 14.1, native)
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the example with Pkl 0.25.2, comparing the typealias form with the direct union form. Start by tracing the typealias cycle diagnostic and the rendering path mentioned in stdlib/base.pkl, then confirm that both forms behave equivalently without the cyclic-alias error. There is no named test file, so add or run the relevant regression coverage if the project identifies one.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.