Giter Club home page Giter Club logo

Comments (6)

TheToor avatar TheToor commented on June 2, 2024 1

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

TheToor avatar TheToor commented on June 2, 2024 1

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.

mklement0 avatar mklement0 commented on June 2, 2024

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

237dmitry avatar 237dmitry commented on June 2, 2024

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.

237dmitry avatar 237dmitry commented on June 2, 2024

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.

mklement0 avatar mklement0 commented on June 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.