Cannot find property #105

Closed
opened 2025-12-30 01:20:50 +01:00 by adam · 3 comments
Owner

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?
adam closed this issue 2025-12-30 01:20:51 +01:00
Author
Owner

@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).

@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)`.
Author
Owner

@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.

@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.
Author
Owner

@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.

@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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#105