Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Created December 19, 2020 21:18
Show Gist options
  • Save IISResetMe/9cae89705dc4e80561582ad19847abd4 to your computer and use it in GitHub Desktop.
Save IISResetMe/9cae89705dc4e80561582ad19847abd4 to your computer and use it in GitHub Desktop.
ECMA-335 SpecialName-based operator overloading in PowerShell classes
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