🐛fixed properties not respected by toTyped() #182

Open
opened 2025-12-30 01:21:49 +01:00 by adam · 3 comments
Owner

Originally created by @thomaspurchas on GitHub (Jul 8, 2024).

The toTyped API doesn't respect fixed properties on classes, resulting in the following unexpected outcome:

class Bird {
  fixed type = "Parrot"
  name = "Polly"
}

untypedBird {
  type = "Cocker spaniel"
  name = "Morris"
}

result = untypedBird.toTyped(Bird)

produces

untypedBird {
  type = "Cocker spaniel"
  name = "Morris"
}
result {
  type = "Cocker spaniel"
  name = "Morris"
}
Originally created by @thomaspurchas on GitHub (Jul 8, 2024). The `toTyped` API doesn't respect `fixed` properties on classes, resulting in the following unexpected outcome: ```pkl class Bird { fixed type = "Parrot" name = "Polly" } untypedBird { type = "Cocker spaniel" name = "Morris" } result = untypedBird.toTyped(Bird) ``` [produces](https://pkl-playground.vercel.app/?share=truck-species-screen) ``` untypedBird { type = "Cocker spaniel" name = "Morris" } result { type = "Cocker spaniel" name = "Morris" } ```
adam added the bug label 2025-12-30 01:21:49 +01:00
Author
Owner

@hmonfleur commented on GitHub (Jul 10, 2024):

I don't think It's particular to fixed properties. I have the same kind of problem but with "normal" properties :

class A{
  x:Int
  y = x + 1
}

a = Map("x", 1).toTyped(A)

produces the following error :

Tried to read property `x` but its value is undefined.

Which is understandable since :

class A{
  x:Int = 0
  y = x + 1
}

a = Map("x", 1).toTyped(A)

produces :

{
  "a": {
    "x": 1,
    "y": 1
  }
}

If I'm missing something or if there is a way around it I'm interested :)

@hmonfleur commented on GitHub (Jul 10, 2024): I don't think It's particular to fixed properties. I have the same kind of problem but with "normal" properties : ```pkl class A{ x:Int y = x + 1 } a = Map("x", 1).toTyped(A) ``` [produces](https://pkl-playground.vercel.app/?share=stretch-away-speed) the following error : ```Tried to read property `x` but its value is undefined.``` Which is understandable since : ```pkl class A{ x:Int = 0 y = x + 1 } a = Map("x", 1).toTyped(A) ``` [produces](https://pkl-playground.vercel.app/?share=earth-me-go) : ``` { "a": { "x": 1, "y": 1 } } ``` If I'm missing something or if there is a way around it I'm interested :)
Author
Owner

@holzensp commented on GitHub (Jul 18, 2024):

The point @thomaspurchas is making - I believe - is that he expected type to be "Parrot". What isn't being respected here, is that toTyped ignores the fixed-ness of the property.

@holzensp commented on GitHub (Jul 18, 2024): The point @thomaspurchas is making - I believe - is that he expected `type` to be `"Parrot"`. What isn't being respected here, is that `toTyped` ignores the `fixed`-ness of the property.
Author
Owner

@hmonfleur commented on GitHub (Jul 23, 2024):

Ok, maybe I wrongly made a link between my problem and the one that @thomaspurchas raised.
Still there's a behavior that is not the one I would have expected when using toTyped , i.e., it does not produce the same object as normal instantiation would have. I have found some partial workaround using let to force evaluation at some points (I mean I think that's what it does) but it does not entirely solve the problem (in my case for instanciation of objects contained in attributes and produced by the instanciation of an object that are not instanciated when using toTyped resulting in empty attributes).
Maybe I can sum that up and open a new issue about it.

@hmonfleur commented on GitHub (Jul 23, 2024): Ok, maybe I wrongly made a link between my problem and the one that @thomaspurchas raised. Still there's a behavior that is not the one I would have expected when using `toTyped` , i.e., it does not produce the same object as normal instantiation would have. I have found some partial workaround using `let` to force evaluation at some points (I mean I think that's what it does) but it does not entirely solve the problem (in my case for instanciation of objects contained in attributes and produced by the instanciation of an object that are not instanciated when using `toTyped` resulting in empty attributes). Maybe I can sum that up and open a new issue about it.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#182