You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add an operator to allow safe checking for property names in Strict Mode #15338
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.
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
Reacted by Patrick Meinecke, 237dmitry, Michael Klement, Dongbo Wang, iRon7, Ben Broderick Phillips, Henry Buckle and Jason Thompson
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.
Activity
iRon7 commentedon Jul 25, 2023
With the latest version of
Set-StrictModethere are even two different kind of strict errors here:The object not exists (is
$Null):$ObjectNotExists.Properyand the property not exists:
$MyObject.ProperyNotExistsMeaning that this probably requires also two enhancements:
If the object potentially not exists (is
$Null):$myObject?.Propertyand 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 commentedon Jan 21, 2024
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.