Make deepToTyped redundant by improving toTyped #239

Open
opened 2025-12-30 01:22:36 +01:00 by adam · 0 comments
Owner

Originally created by @holzensp on GitHub (Nov 1, 2024).

Currently, toTyped is shallow. For example, given

class Bird {
  name: String
  diet: Listing<String>
}

local example: Dynamic = new {
  name = "Pigeon"
  diet {
    "seeds"
    "fast food"
  }
}

succeeds = example.toTyped(Bird).name
fails = example.toTyped(Bird)

would correctly produce

succeeds = "Pigeon"

if it did not throw on fails:

–– Pkl Error ––
Expected value of type Listing, but got type Dynamic.
Value: new Dynamic { ?; ? }

3 | diet: Listing<String>
          ^^^^^^^^^^^^^^^
at repl#Bird.diet (repl:pkl0)

This is why deepToTyped was developed. However, toTyped should recursively convert to the required typed structure.

By default this should retain laziness.

Originally created by @holzensp on GitHub (Nov 1, 2024). Currently, `toTyped` is _shallow_. For example, given ``` class Bird { name: String diet: Listing<String> } local example: Dynamic = new { name = "Pigeon" diet { "seeds" "fast food" } } succeeds = example.toTyped(Bird).name fails = example.toTyped(Bird) ``` would correctly produce ``` succeeds = "Pigeon" ``` if it did not throw on `fails`: ``` –– Pkl Error –– Expected value of type Listing, but got type Dynamic. Value: new Dynamic { ?; ? } 3 | diet: Listing<String> ^^^^^^^^^^^^^^^ at repl#Bird.diet (repl:pkl0) ``` This is why [`deepToTyped`](https://pkl-lang.org/package-docs/pkg.pkl-lang.org/pkl-pantry/pkl.experimental.deepToTyped/current/index.html) was developed. However, `toTyped` _should_ recursively convert to the required typed structure. By default this should retain laziness.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#239