[feat] module self type allow submodule definition inline #111

Open
opened 2025-12-30 01:20:55 +01:00 by adam · 1 comment
Owner

Originally created by @othompson2 on GitHub (Mar 10, 2024).

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
Originally created by @othompson2 on GitHub (Mar 10, 2024). 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 ```
Author
Owner

@bioball commented on GitHub (Mar 14, 2024):

Thanks! Looks like default value for the module type does not work.

@bioball commented on GitHub (Mar 14, 2024): Thanks! Looks like default value for the `module` type does not work.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#111