We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing information
It seems hard to create a switch on types.
switch
The following doesn't work:
$var = @{A = $true; B = 'abc'} foreach ($key in $var.Keys) { switch ($var[$key].GetType()) { [bool] { if ($var[$key]) { $key } } [string] { "$key: $($var[$key])" } } }
May I suggest to add information for this case to the below mentioned documentation page?
https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-switch?view=powershell-7.4
https://github.com/MicrosoftDocs/PowerShell-Docs/blob/main/reference/docs-conceptual/learn/deep-dives/everything-about-switch.md
@sdwheeler
9c03bc13-a80c-0a4c-0537-81235d4a849c
The text was updated successfully, but these errors were encountered:
The type is being parsed as a typecast on the scriptblock. Enclose the matching condition in a script block to avoid the typecast.
switch ($var[$key].GetType()) { {$_ -eq [bool]} { if ($var[$key]) { $key } } {$_ -eq [string]} { "$key`: $($var[$key])" } }
Sorry, something went wrong.
Thanks for clarifying, @sdwheeler!
Coincidentally, I just found another GitHub issue, proposing an improvement to the PowerShell language: PowerShell/PowerShell#17456
It claims that it's sufficient to surround the type declaration with parentheses:
switch ($var[$key].GetType()) { {[bool]} { if ($var[$key]) { $key } } {[string]} { "$key`: $($var[$key])" } }
I just tested it, and it works.
Perhaps the documentation may give some kind of rule or explanation on when and why parentheses are required in a switch statement condition.
It looks like omitting the parentheses is more or less some kind of special case?
Fixes #10911 - Add example using an expression (#10913)
7d0a67e
* Add example using an expression * Improved the example
sdwheeler
Successfully merging a pull request may close this issue.
Type of issue
Missing information
Feedback
It seems hard to create a
switch
on types.The following doesn't work:
May I suggest to add information for this case to the below mentioned documentation page?
Page URL
https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-switch?view=powershell-7.4
Content source URL
https://github.com/MicrosoftDocs/PowerShell-Docs/blob/main/reference/docs-conceptual/learn/deep-dives/everything-about-switch.md
Author
@sdwheeler
Document Id
9c03bc13-a80c-0a4c-0537-81235d4a849c
The text was updated successfully, but these errors were encountered: