question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type-constrained Boolean variables fail to implicitly convert from most types, such as PSObject / PSCustomObject instances

See original GitHub issue

Steps 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:open
  • Created 4 years ago
  • Reactions:2
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
vexx32commented, Sep 28, 2020

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.

1reaction
vexx32commented, Sep 17, 2020

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found