apple / apple/pkl

[feat] `module` self type allow submodule definition inline

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

Description

I am trying to define and type a variable using the `module` self type, however when I attempt to use it I encounter an error. The error suggests it is trying to match to the module type so the self type is working, however its not able to implicitly map the `Dynamic` type to it, this can be resolved by explicitly typing it.

I feel like the expected behaviour would be that they do the same thing, however it appears the `module` keyword currently only supports importing another file rather than defining the sub-module in place. In most circumstances would be fine to switch to the other syntax, but I am trying to define the `example` variable in a module and then extend it before amending it - so I need to self-type for it to work.

---
### Error
```
// Bird.pkl
example: module?
test: Boolean?
```
When I try to use variable `example` in a module which amends `Bird.pkl`
```
// example.pkl
amends "Bird.pkl"

example {
test = true
}
```
I get the following error:
```
–– Pkl Error ––
Expected value of type `Bird`, but got type `Dynamic`.
Value: new Dynamic { test = ? }

2 | example: module?
^^^^^^
at Bird#example (file:///Bird.pkl, line 2)

4 | example {
^^^^^^^^^
at example#example (file:///example.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 I update the `example` variable with an explicit self reference the error goes away and it functions as expected.
```
// Bird.pkl
import "Bird.pkl"

example: Bird?
test: Boolean?
```

outputs:
```
example {
example = null
test = true
}
test = null
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the behavior using the issue's Bird.pkl and example.pkl snippets, comparing the inline `module?` declaration with the explicit `Bird?` self-reference. Start by tracing how `example` is typed and resolved before `renderer.renderDocument` in `stdlib/base.pkl` line 106; done means inline submodule definition works without an explicit self-reference and retains the shown output behavior.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
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.