-
Notifications
You must be signed in to change notification settings - Fork 339
Add Object.toMixin() method #1257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this not be done in userland Pkl?
function dynamicToMixin(mix: Dynamic): Mixin = new { ...mix }It's likely that the "deep" variant of this that recursively turns a nested object structure (and not just a Dynamic!) into a Mixin isn't userland-friendly, but I'm not sure there needs to be an API for the shallow implementation.
Missed the implementation detail that makes this work, never mind :)
| // If the key is a Long (element index), offset it by parentLength | ||
| if (key instanceof Long) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a sound assumption to make. Mappings and Dynamics can have entries with Int (in Pkl, Long in Java) keys. Probably better to check member.isElement() here instead (and you can then assume the key is a Long).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be the internal field representation, not the userland defined keys. I will see if I can get the key to be the right type at compile time and replace this check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated to if (member.isElement()) {
|
@HT154 I don't believe so due to how amends vs replacements are constructed. Consider this example: local base = new {
a1 {
b1 = 2
}
a2 {
b1 = 2
}
}
local over = new Mixin {
a1 = new Dynamic {
b2 = 2
}
a2 {
b2 = 2
}
}
local overrideValue = new Dynamic {} |> over
function dynamicToMixin(mix: Dynamic): Mixin = new {
...mix
}
actualValue = base |> dynamicToMixin(overrideValue)
expectedValue = base |> overWhich evaluates to actualValue {
a1 {
b2 = 2
}
a2 { // ❗️This is not correct, the original Mixin has amend semantics here, not replace
b2 = 2
}
}
expectedValue {
a1 {
b2 = 2
}
a2 {
b1 = 2
b2 = 2
}
} |
Adds a toMixin() method to the Object class that converts an Object into a Mixin function applicable via the pipe operator (|>). A generic toMixin() method cannot be implemented in user land, so this implementation provides a native method that properly handles: - Property merging and overriding - Element appending with correct index offsetting - Entry merging with proper key handling - Nested object replacement vs amendment semantics Implementation uses the source Object's enclosing frame to ensure proper module context for type resolution during member evaluation.
|
Updated to implement on |
Adds a
toMixin()method to theObjectclass that converts anObjectinto a
Mixinfunction applicable via the pipe operator (|>).A generic
toMixin()method cannot be implemented in user land, so thisimplementation provides a native method that properly handles:
Implementation uses the source
Object's enclosing frame to ensureproper module context for type resolution during member evaluation.
edit: updated from
DynamictoObject