pkl
pkl copied to clipboard
Using type alias causes error where directly using the type doesn't
It is surprising (at least to me) that in the following pkl definition:
typealias Step = GetStep | PutStep
class Job {
steps: Listing<Step>?
}
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<Step>?
^^^^
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:
class Job {
steps: Listing<GetStep | PutStep>?
}
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)
We are discussing the possibility to create recursive type aliases. But, indeed, this is not currently supported.