pkl icon indicating copy to clipboard operation
pkl copied to clipboard

Make `deepToTyped` redundant by improving `toTyped`

Open holzensp opened this issue 1 year ago • 0 comments

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.

holzensp avatar Nov 01 '24 17:11 holzensp