PowerShell Function Names

PowerShell allows you to use many different characters in your function names besides [_aA-zZ][_aA-zA0-9], which is the typical regex recipe for function and method identifiers in a fair number of languages.  However, in PowerShell you have a much larger palette of characters to choose from e.g.:

function ?? ($expr, $default = $(throw "Must specify default value")) {
if ($expr -ne $null) {
else {

Function names like these can help you create operators for an internal DSL.  The primary downside is that the operator has to be used in a pre-fix manner e.g.:

PS> ?? ($env:LogDir) $env:temp

Things get really interesting when you start to use mathematical symbols.  Check this out:

PS> function √($num) { [Math]::Sqrt($num) }
PS> √ 81
PS> function φ { (1 + (√ 5)) / 2 }
PS> φ

This is not really practical since the mathematical symbol characters (0x221A and 0x3C6) aren’t easy to type at the console but it shows the extent to which you can use radically different characters to name your functions.

psmdtag:script – function name

This entry was posted in PowerShell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s