Switch fails with null value
See original GitHub issueSteps to reproduce
$var = get-process | out-null
switch ($var) {
$null { write-host NULL }
default { write-host DEFAULT }
}
In my specific case, I assign $var to the output of a native command that, in some expected cases, doesn’t return anything. Note that explicitly assigning $var = $ null appears to be not the same thing and doesn’t trigger the issue.
Expected behavior
Expecting the switch to be evaluated and the $null clause honored.
Actual behavior
The switch is entirely skipped.
Environment data
PSVersion 6.2.2
PSEdition Core
GitCommitId 6.2.2
OS Microsoft Windows 10.0.18362
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6
Top Results From Across the Web
How to use null in switch - java
If the switch expression is of a reference type, such as a boxed primitive type or an enum, a run-time error will occur...
Read more >Bruce Eckel on switch expressions, arrow syntax, and case ...
Java says that a switch must cover all possible values even if it does not cover null. This is a backward-compatibility issue: If...
Read more >Switch function and null values - Power Platform Community
Hello, I'm trying to use a switch function based on some Dataverse data/lookup field, however some of the data values are null.
Read more >{Solved} The execution of template action 'Switch' failed: It is of ...
Inside a switch-case, if the condition expression evaluate to null, the switch-case construct won't be able to handle null value and will throw ......
Read more >null occurs as more than one when branch for this switch ...
The value null is a legal value for all types. Each when value must be unique. For example, you can use the literal...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@EPinci the output of a native command cannot result in a true $null assignment, because native commands only deal with text output. You can get “nothing”, you can sometimes in odd cases get an empty string value (
[string]::Empty
or''
), or a string containing just whitespace.Native commands don’t deal with the CLR, so they don’t have the ability to pass a $null value to another utility like PowerShell.
More like “no results”. It acts a bit differently than an unassigned variable in some cases. It’s a bit like how a SQL query could return a literal
NULL
value or just not find any matching results.AutomationNull
is like a mix between an empty collection (e.g.@()
)and$null
. A bit closer to an empty collection than$null
.