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

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

Closed
vexx32 opened this issue May 3, 2021 · 2 comments
Closed
Labels
Issue-Enhancement the issue is more of a feature request than a bug Resolution-No Activity Issue has had no activity for 6 months or more WG-Language parser, language semantics

Comments

@vexx32
Copy link
Collaborator

vexx32 commented May 3, 2021

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
@vexx32 vexx32 added Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. labels May 3, 2021
@daxian-dbw daxian-dbw added WG-Language parser, language semantics and removed Needs-Triage The issue is new and needs to be triaged by a work group. labels May 3, 2021
@iRon7
Copy link

iRon7 commented Jul 25, 2023

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?

@microsoft-github-policy-service microsoft-github-policy-service bot added the Resolution-No Activity Issue has had no activity for 6 months or more label Jan 21, 2024
Copy link
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
Labels
Issue-Enhancement the issue is more of a feature request than a bug Resolution-No Activity Issue has had no activity for 6 months or more WG-Language parser, language semantics
Projects
None yet
Development

No branches or pull requests

3 participants