Comments (6)
@237dmitry
Default is not executed in that case. That is the problem.
$Test = $Null | Out-Null
switch($Test) {
$null { Write-Host "It's null" }
Default { Write-Host "It's Default" }
}
Produces no output when the default case should have been run.
from powershell.
Thanks @mklement0 for the detailed explaination.
Digging through the other proposal and issues this is probably a case not often encountered but still results in confusing behaviour when it happens.
Any plans to reopen the conversation of your proposed implementation?
from powershell.
$Null | Out-Null
doesn't return $null
; any cmdlet (or function / script file / script block) - such as Out-Null
- that produces no output technically returns the [System.Management.Automation.Internal.AutomationNull]::Value
singleton, the "enumerable null", if you will.
In an enumeration context - including input provided to a switch
statement - [System.Management.Automation.Internal.AutomationNull]::Value
behaves like an empty enumerable; that is, no iterations take place; the switch
statement is effectively skipped.
That is, it behaves like the following statement: switch (@()) { Default { 'never get here' } }
: enumerating the empty array results in no elements, so the statement body is never entered.
What is tricky is that it isn't easy to discover whether a given variable value is a bona fide $null
or [System.Management.Automation.Internal.AutomationNull]::Value
, especially given that in expression contexts as well as in parameter-binding, the latter is effectively the same as $null
.
#13465 is a green-lit proposal to make discovery of [System.Management.Automation.Internal.AutomationNull]::Value
easier, via $Test -is [System.Management.Automation.AutomationNull]
, but it hasn't been implemented yet; also, it was inappropriately closed by the bot.
from powershell.
default
is executed when no switches are found that match the condition.
$null -eq $null # True
undefined state
$a = $null
Get-Variable a
from powershell.
Default is not executed in that case. That is the problem.
This is because there is a $null switch, which is equivalent to $Test and returns $true. If you have a match, then default
will not be executed.
Look at full syntax:
$ & {
$test = $null
switch ($test)
{
{$_ -eq 1} { 1 }
{$_ -eq 'one'} { 'one' }
{$_ -eq $null} { 'null' }
default { 'default' }
}
}
null
$
from powershell.
Glad to hear it helped, @TheToor; I have no say in what gets reopened, but I generally think that all auto-closed issues that have the Up-for-Grabs
label should be reopened, as previously suggested in #21406 (comment).
from powershell.
Related Issues (20)
- Get-LocalGroupMember - Failed to compare two elements in the array, v2 HOT 2
- Unable to install PS on Ubuntu server 20.04 LTS - package dependency update HOT 1
- (some) command read from stdin seem to require extra trailing newline HOT 4
- New-LocalUser doesn't work in Power 7.3+ without importing Microsoft.Powershell.LocalAccounts
- Get-ChildItem overwrites a file's Name property HOT 2
- exit status `0` on syntax errors, when reading scrip from stdin HOT 4
- `$ErrorActionPreference = 'Stop'` ignored, when command read from stdin. HOT 12
- `-CommandWithArgs` CLI parameter doesn't work with `-` (code provided via stdin)
- fwlink helpURI of Get-Uptime,Get-FileHash and others doesn't work HOT 5
- Unable to Install PowerShell 7.4 with Ubuntu 24.04 HOT 6
- Incorrect Auto-Completion When Using Tab HOT 4
- Mount-DiskImage does not assign driver letter every third time HOT 2
- Does PowerShell support the real "Stream" response? HOT 5
- v7.4.2 not in Ubuntu 22.04 Linux package repository for Microsoft products HOT 3
- `Finally` block isn't executed upon Ctrl+C HOT 36
- PSReadLine gets disabled when using Intel UHD Graphics driver v31.0.101.5448
- Make `ConvertFrom-StringData` more error tolerant HOT 9
- Add-Type ReferencedAssemblies only works with full file paths now? HOT 1
- .Net 8 in PowerShell 7 System.Private.CoreLib fails to register. HOT 41
- `ConvertFrom-StringData` mistakenly emits a statement-terminating error when one of its input strings isn't well-formed HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from powershell.