question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Switch fails with null value

See original GitHub issue

Steps 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
vexx32commented, Aug 7, 2019

@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.

1reaction
SeeminglySciencecommented, Aug 7, 2019

If I get it right… the “AutomationNull” is equal to “unassigned”?

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found