Type-constrained Boolean variables fail to implicitly convert from most types, such as PSObject / PSCustomObject instances
See original GitHub issueSteps to reproduce
# OK: convert via cast (assigns $true)
$b = [bool] [pscustomobject]::new()
# !! BROKEN: convert via type constraint
[bool] $b = [pscustomobject]::new()
Expected behavior
No output. Both assignments should succeed.
Actual behavior
The 2nd statement causes the following error:
Cannot convert value "System.Management.Automation.PSCustomObject" to type "System.Boolean".
Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0
Environment data
PowerShell Core 7.0.0-preview.3
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Everything you wanted to know about PSCustomObject
PSCustomObject is a great tool to add into your PowerShell tool belt. Let's start with the basics and work our way into the...
Read more >Powershell - Cannot convert value of type "System. ...
I obtain the following error: Cannot convert the "@{Path=some path string here; Pack=False}" value of type "System.Management.Automation.
Read more >Chapter 3. Working with types - Windows PowerShell in Action ...
Most shell environments can only deal with strings, so the ability to use objects makes PowerShell different. Where you have objects, you also...
Read more >Chapter 10. Metaprogramming with scriptblocks and dynamic ...
Scriptblock basics. In PowerShell, the key to metaprogramming is the scriptblock. This is a block of script code that exists as an object...
Read more >Windows PowerShell in Action
Consider this book the definitive reference for PowerShell. As one of the designers of the PowerShell environment, the author knows all the ins...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Seems to me the logic would be better placed in the binder logic itself, rather than in variable assignment. Might be a bit of work to do, but IMO typical variable assignments should not be half mixed up in what should be binder-specific logic. The binder does a fair bit of extra logic as it is already, it’s kind of weird that this (and seemingly this alone) got thrown into the standard variable assignment logic.
Given that this also doesn’t happen with any other type, I think it makes sense to remove this special case. See #13651 for a related issue that arises because of this special casing.
/cc @SteveL-MSFT @rjmholt can we get y’all’s thoughts on this one?