Created
December 19, 2020 21:18
-
-
Save IISResetMe/9cae89705dc4e80561582ad19847abd4 to your computer and use it in GitHub Desktop.
ECMA-335 SpecialName-based operator overloading in PowerShell classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MagicNumber | |
{ | |
hidden [int] $_value | |
MagicNumber([int]$value) | |
{ | |
$this._value = $value | |
} | |
# ECMA-335 I.10.3.2 | |
static hidden | |
[MagicNumber] op_Addition([MagicNumber]$a, [MagicNumber]$b) | |
{ | |
return [MagicNumber]::new($a._value + $b._value) | |
} | |
# ECMA-335 I.10.3.3 | |
static hidden | |
[int] op_Implicit([MagicNumber]$n) | |
{ | |
return $n._value | |
} | |
[string] | |
ToString() | |
{ | |
return "Zero One Two Three Four Five Six Seven Eight Nine".Split()[$this._value % 10] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment