Skip to content

Add an operator to allow safe checking for property names in Strict Mode #15338

Closed

Description

Summary of the new feature/enhancement

Currently Strict Mode is very difficult to use effectively, as there isn't really a good way to safeguard code against errors that may arise from objects not having certain properties. Given how mutable objects can be in powershell, this makes working in strict mode more frustrating than it needs to be.

Proposed technical implementation details (optional)

Currently the only real way to check this is with the somewhat obscure $object.PSObject.Properties.Name -contains $propName -- an operator should be provided that checks whether the property exists in a similar way (can we be more thorough than this? Should we?) and safely returns a null or false so that these expressions are easier to write.

Temporarily borrowing the ?. operator for now (we don't have to use this operator, but at least to me it sort of makes sense here), it would look something like this:

$value = if ($myObj?.Property) {
    $myObj.Property
}
else {
    $safeDefault
}

# nice and neat if we use null coalescing too

$value = $myObject?.Property ?? $safeDefault

Activity

  1. added
    WG-Languageparser, language semantics
    and removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on May 3, 2021
  2. iRon7 commented on Jul 25, 2023

    @iRon7

    With the latest version of Set-StrictMode there are even two different kind of strict errors here:

    $MyObject = [PSObject]@{ Property = 1 }
    Set-StrictMode -Version Latest

    The object not exists (is $Null):

    $ObjectNotExists.Propery

    InvalidOperation: The variable '$ObjectNotExists' cannot be retrieved because it has not been set.

    and the property not exists:

    $MyObject.ProperyNotExists

    PropertyNotFoundException: The property 'ProperyNotExists' cannot be found on this object. Verify that the property exists.

    Meaning that this probably requires also two enhancements:
    If the object potentially not exists (is $Null):

    $myObject?.Property

    and if the property potentially not exists:

    $myObject.Property?

    Or where potentially both the object and the property might not exist:

    $myObject?.Property?
  3. microsoft-github-policy-service commented on Jan 21, 2024

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions