Operator Overloading with Script Methods #26957
Replies: 4 comments
-
Without a good consensus what is expected from each combination of operator and related (non-primitive) operants, I personally think it will open up an incredibly large number of pitfalls and caveats. Anyways, the error messages you get from your wishful thinking:
contains the hint to how you my create your prototypes or custom types: Class MyCustomObject : System.Management.Automation.PSObject {
static [MyCustomObject]op_Addition([Object]$Object, [Int]$Value) {
$PSObject = $Object.PSObject.Copy()
$PSObject.Value = $PSObject.Value + $Value
return $PSObject
}
static [MyCustomObject]op_Subtraction([Object]$Object, [Int]$Value) {
$PSObject = $Object.PSObject.Copy()
$PSObject.Value = $PSObject.Value - $Value
return $PSObject
}
MyCustomObject() {}
}
$foo = [MyCustomObject]@{PSTypeName='foo';Value=42}
$foo += 1
$foo.Value # returns 43
$bar = $foo - 5
$bar
Value
-----
38 |
Beta Was this translation helpful? Give feedback.
-
|
I think defining it in a class is a mostly acceptable workaround. From a performance perspective and a "don't upset the applecart" perspective, this would be acceptable. From an "easy of use" perspective, I suspect far more people use Add-Member than classes. Since any individual writing a script or module would be overloading the operators for their own types, I don't believe it would cause catastrophe either way. Thanks for showing it can be done with classes though! |
Beta Was this translation helpful? Give feedback.
-
|
The Engine WG discussed this issue. We see the value in the concept of allowing ETS to override arithmetic operators, but on its own it's not a feature we think meets the bar for impact at this time. We may consider improvements to ETS in the future that could include this feature in some way. We are converting this issue to a discussion to gather more feedback and use cases for potential future design work. |
Beta Was this translation helpful? Give feedback.
-
|
@SeeminglyScience thanks for the update! I would also like to know the status of working group membership / discussions. A few years ago @SteveL-MSFT asked which working groups I might want to be on. I replied (including this WG) and yet, have still not been "looped in" Additionally, I have requested that working group meetings that involve a particular feature / issue invite the people to speak. I would have liked to see this happen in this case. I can appreciate that this fits into a "not now, but maybe sometime" bucket. I would have preferred some communication on the issue in the past 9 months. 🤷 Better late than never. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary of the new feature / enhancement
The recent string of issues makes me wish for a more generalized feature.
Many .NET classes do not support operator overloading, but could be extensibly overridden in PowerShell.
Since PowerShell has an extended type system, we can effective add a static script method to any class.
I propose that PowerShell should check for a ScriptMethod overload when a compiled operator is not found, and use this instead.
This would open up an incredibly large number of doors for PowerShell, as overloading operators is currently impossible in pure PowerShell.
Proposed technical implementation details (optional)
Let's imagine we wanted to make a PSObject that could add.
Either approach should theoretically work, though using Update-TypeData is more likely to be quick
Beta Was this translation helpful? Give feedback.
All reactions