Skip to content
New issue

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

Introduce a new conceptual topic that explains to-Boolean coercion logic ("truthiness") #3356

Closed
mklement0 opened this issue Nov 29, 2018 · 1 comment · Fixed by #7697
Closed
Assignees
Labels
issue-doc-idea Issue - request for new content Pri3 Priority - Low up-for-grabs Tag - issue is open for any contributor to resolve

Comments

@mklement0
Copy link
Contributor

PowerShell can treat any type implicitly as a Boolean, which is generally very handy, but can be problematic if the rules aren't clear.

The rules are summarized below, and I hope they'll become the basis for a new conceptual topic such as about_Booleans.

The following topics should then be updated to point to the new topic:

  • about_If
  • about_While
  • about_Do
  • about_Arrays (the section about the .Where() method)
  • Where-Object
  • Others?

To-Boolean conversion summary:

  • Among scalars:
    • The following are implicitly $false:

      • ''/"" (empty string)
      • $null
      • 0 (of any numeric type).
    • Pitfall: Any non-empty string evaluates to $true

      • e.g., [bool] 'False' is $true
      • Note that this differs from explicit string parsing: [bool]::Parse('false') does return$false (and $true for 'true', but recognizes nothing else).
    • Instances of any other (non-collection) type are implicitly $true, including of type [pscustomobject].

  • Among collections such as arrays (more accurately, collection-like types that implement the IList interface - see the source code):
    • Empty collections are always $false, as is the special "null collection" value indicating the absence of output from a command, [System.Management.Automation.Internal.AutomationNull]::Value.
    • Pitfall: Single-element collections evaluate to the Boolean value of their one and only element.
    • 2+-element collections are always $true.
@sdwheeler
Copy link
Contributor

NOTE: Type conversion is covered in detail in Chapter 6 of the PowerShell Language Specification. It may be best to just convert that chapter to docs.

@sdwheeler sdwheeler self-assigned this Jun 10, 2021
sdwheeler added a commit that referenced this issue Jun 10, 2021
* Fixes #3356 - add about_Booleans article

* fix broken link

* fix typos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue-doc-idea Issue - request for new content Pri3 Priority - Low up-for-grabs Tag - issue is open for any contributor to resolve
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants