mirror of
https://github.com/apple/pkl.git
synced 2026-01-11 22:30:54 +01:00
default values not being used in mapping when explicitly calling new on Object #129
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @harryjackson on GitHub (Mar 30, 2024).
I created a Mapping with some default values for the object values and in some cases the values get used and in others they don't.
I've created an example of what I mean here. The code as is should work if you run
Note the differences between lines 57 and 61, now comment out line 62 and "pkl eval defaults.pkl" fails. Can someone explain the reason the explicit call to "new Service.ServicePort" means the defaults are no longer used?
@HT154 commented on GitHub (Apr 2, 2024):
This comes down to what is being amended. There's actually yet another option here that looks like this:
Here's what each of these idioms means in (more) plain language:
= new {- amend the default value as defined in the Mapping/Listing/class= new Service.ServicePort {- creates a full newService.ServicePort(amending only that class's default values)It's a little easier to see it in action, here's a more complete example:
This evaluates to:
Using
defaultin a Mapping or Listing works similarly to the class default value I used in this example:This evaluates to:
@holzensp commented on GitHub (Apr 3, 2024):
Totally what @HT154 said. Also, looking at your code, I'd suggest substituting lines 50-65 with
targetPort = port; that's unchanged from the defaultMapping<K,V>.defaultis(K) -> V; it's a function, so you can use the value of the key to express the default value of the entry.Also, the property-by-property copy (lines 28-34) confuses me. Do you want everything as in
sp, except thenodePort?@harryjackson commented on GitHub (Apr 6, 2024):
@holzensp Another lovely feature that I'm using a lot now.
@HT154 You need to write that up in the documentation somewhere. I can see how each of the options could be used as a feature and your description is great.
@HT154 commented on GitHub (Apr 9, 2024):
I'd love to get this into the docs! I'll defer to @holzensp and crew on where the best place to put it is. Maybe a new section under the language reference's Advanced Topics?
@HT154 commented on GitHub (Oct 15, 2024):
Details on this behavior were added to the language reference in https://github.com/apple/pkl/pull/624