Voo
Voo

Reputation: 30235

Return in switch block behaves differently between Core and Classic PowerShell

The following script gives different results when it is run on 5.1 compared to 7.4.0.

function foo($x) {
    $val = switch ($x){
        1 { 5 }
        default {
            return 10 
        }
    }
    Write-Host "Val is $val" 
    $val + 1
}

Write-Host "foo 1: $(foo 1)" 
Write-Host "foo 2: $(foo 2)" 

PS 5.1 output:

Val is 5
foo 1: 6
foo 2: 10

PS 7.4.0 output:

Val is 5
foo 1: 6    
foo 2:

Is this a bug in PS Core (it's certainly unexpected to me) or is this a documented change/was always undefined behavior?

Upvotes: 6

Views: 119

Answers (1)

Voo
Voo

Reputation: 30235

This is a bug in PowerShell 7.4.0 (which will hopefully be fixed in the next release).

See this bug report.

Upvotes: 0

Related Questions