Originally created by @philippemerle on GitHub (Mar 4, 2024).
Following file is correct.
class Person {
name: String
}
function buildPersons(names: Set<String>): Listing<Person>
= new {
for(n in names) {
new Mapping {
["name"] = n
}.toMap().toTyped(Person)
}
}
persons = buildPersons(Set("Me", "You"))
But if this file is split into 2 files then there is an error.
File base_template.pkl
persons: Listing<Person>
class Person {
name: String
}
File my_template.pkl
amends "base_template.pkl"
persons = buildPersons(Set("Me", "You"))
local function buildPersons(names: Set<String>): Listing<Person>
= new {
for(n in names) {
new Mapping {
["name"] = n
}.toMap().toTyped(Person)
}
}
Then evaluating my_template.pkl produces the following error:
–– Pkl Error ––
Cannot find property `Person`.
10 | }.toMap().toTyped(Person)
^^^^^^
at my_template#buildPersons[#1] (file:///.../my_template.pkl, line 10)
Did you mean any of the following?
persons
buildPersons
3 | persons = buildPersons(Set("Me", "You"))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at my_template#persons (file:///.../my_template.pkl, line 3)
106 | text = renderer.renderDocument(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (https://github.com/apple/pkl/blob/0.25.2/stdlib/base.pkl#L106)
Here, pkl seems to not see the Person class.
Do I miss something?
Originally created by @philippemerle on GitHub (Mar 4, 2024).
Following file is correct.
```
class Person {
name: String
}
function buildPersons(names: Set<String>): Listing<Person>
= new {
for(n in names) {
new Mapping {
["name"] = n
}.toMap().toTyped(Person)
}
}
persons = buildPersons(Set("Me", "You"))
```
But if this file is split into 2 files then there is an error.
File `base_template.pkl`
```
persons: Listing<Person>
class Person {
name: String
}
```
File `my_template.pkl`
```
amends "base_template.pkl"
persons = buildPersons(Set("Me", "You"))
local function buildPersons(names: Set<String>): Listing<Person>
= new {
for(n in names) {
new Mapping {
["name"] = n
}.toMap().toTyped(Person)
}
}
```
Then evaluating `my_template.pkl` produces the following error:
```
–– Pkl Error ––
Cannot find property `Person`.
10 | }.toMap().toTyped(Person)
^^^^^^
at my_template#buildPersons[#1] (file:///.../my_template.pkl, line 10)
Did you mean any of the following?
persons
buildPersons
3 | persons = buildPersons(Set("Me", "You"))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at my_template#persons (file:///.../my_template.pkl, line 3)
106 | text = renderer.renderDocument(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (https://github.com/apple/pkl/blob/0.25.2/stdlib/base.pkl#L106)
```
Here, pkl seems to not see the `Person` class.
Do I miss something?
Why are you creating a Mapping instead of instantiating a Person directly?
local function buildPersons(names: Set<String>): Listing<Person> = new {
for(n in names) {
new Person {
name = n
}
}
}
But the answer is the same as your previous issue: scoping. Try doing .toTyped(module.Person).
@stackoverflow commented on GitHub (Mar 4, 2024):
Why are you creating a `Mapping` instead of instantiating a `Person` directly?
```
local function buildPersons(names: Set<String>): Listing<Person> = new {
for(n in names) {
new Person {
name = n
}
}
}
```
But the answer is the same as your previous issue: scoping. Try doing `.toTyped(module.Person)`.
Thank you for the provided solution. But this should be simpler without module.prefix.
Why are you creating a Mapping instead of instantiating a Person directly?
In fact, my code is more complex as it builds objects dynamically. The provided example is just to illustrate the scoping issue.
@philippemerle commented on GitHub (Mar 4, 2024):
Thank you for the provided solution. But this should be simpler without `module.`prefix.
> Why are you creating a Mapping instead of instantiating a Person directly?
In fact, my code is more complex as it builds objects dynamically. The provided example is just to illustrate the scoping issue.
@bioball commented on GitHub (Mar 4, 2024):
Closing this; see https://github.com/apple/pkl/issues/288#issuecomment-1977057061 for details about the decisions we made behind Pkl's scoping rules.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Originally created by @philippemerle on GitHub (Mar 4, 2024).
Following file is correct.
But if this file is split into 2 files then there is an error.
File
base_template.pklFile
my_template.pklThen evaluating
my_template.pklproduces the following error:Here, pkl seems to not see the
Personclass.Do I miss something?
@stackoverflow commented on GitHub (Mar 4, 2024):
Why are you creating a
Mappinginstead of instantiating aPersondirectly?But the answer is the same as your previous issue: scoping. Try doing
.toTyped(module.Person).@philippemerle commented on GitHub (Mar 4, 2024):
Thank you for the provided solution. But this should be simpler without
module.prefix.In fact, my code is more complex as it builds objects dynamically. The provided example is just to illustrate the scoping issue.
@bioball commented on GitHub (Mar 4, 2024):
Closing this; see https://github.com/apple/pkl/issues/288#issuecomment-1977057061 for details about the decisions we made behind Pkl's scoping rules.