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
Indexing doesn't work for a Hashtable with an array key #9580
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
Yes. Within the context of the repro steps, $hash. $s works as expected. $hash[$s] doesn't work here because powershell incorrectly treating the array $s as slicing indexes.
Dongbo Wang (@daxian-dbw) It isn't "incorrectly" treating the array as a slice. This is by design. We wanted symmetric behavior between indexables wrt slicing. It does mean that you can't index using a collection reference but that was viewed as being a fairly obscure secondary scenario. The workaround is to simply use the item property:
PS[1] (20) >$a=@{($index1= (1,2,3)) ="one"; ($index2= (4,5,6)) ="two"}
PS[1] (21) >$a.item($index1)
one
PS[1] (22) >$a.item($index2)
two
PS[1] (23) >
Note that changing the current behaviour would be a significant breaking change so I'm not sure what you are proposing here.
And this is why an array should not be the key of a hashtable. Item() obviously solves the problem, but ONLY if you know that the key is an array. If you were passed both the key and the hash, you may not be fully away of what methods you can use.
Tbh, I generally tend to expect that the hashtable literal syntax wouldn't accept anything other than a string as a key name, and am then generally surprised when it does strange things like this.
I almost want to say it's worth breaking some of the more esoteric scripts and restricting the literal syntax to only use value types as keys; after all, using a reference type as a key is... gonna be weird at the best of times. You need a reference to that item to get the item back from the table, at which point it almost becomes pointless to keep the data in a hashtable anyway.
Right. And if it's your code, then you should know that. If it's a 3rd party library, then you need to know how that library works in order to use it properly. I would recommend against doing this in a public interface because it is confusing.
Rain Sallow (/u/ta11ow) (@vexx32) Restricting the keys to non-mutable types (not just value types since strings are references) would have make sense but we didn't think about that way back in V1.
Just to clarify, that hashtable comes from .NET right? So the same class can be used in the same way from other .NET languages (though their syntax might be more conventional (key=object, value=object))? So while PowerShell could have syntactically prevented a non-mutable key, that would not have prevented someone in a C# cmdlet from producing one?
Yes, I'm talking only about the PowerShell literal syntax. Even in PS, you could still opt to add weird keys to your hashtable via the $ht.Add() method if you choose.
I'm just thinking it probably makes the most sense for PS not to support that in its literal hashtable declarations due to the esoteric and confusing nature of how it works with objects that can only be compared by reference.
Right, but considering it's still for most intents and purposes utterly arcane and unusable I think that would be a change worth making, no?
While I agree that it's difficult to find a use case for an array as a key, I don't agree as a general rule for all reference types. For example a reference type:
Whose instances are created only once and then cached (e.g. System.RuntimeType)
That overrides GetHashCode and the equality operators/methods (though, hopefully it would also be immutable in design)
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
Activity
msftrncs commentedon May 12, 2019
Do I understand right though, that
$hash. $sworks?daxian-dbw commentedon May 12, 2019
Yes. Within the context of the repro steps,
$hash. $sworks as expected.$hash[$s]doesn't work here because powershell incorrectly treating the array$sas slicing indexes.A slicing index example:
bpayette commentedon May 16, 2019
Dongbo Wang (@daxian-dbw) It isn't "incorrectly" treating the array as a slice. This is by design. We wanted symmetric behavior between indexables wrt slicing. It does mean that you can't index using a collection reference but that was viewed as being a fairly obscure secondary scenario. The workaround is to simply use the
itemproperty:Note that changing the current behaviour would be a significant breaking change so I'm not sure what you are proposing here.
msftrncs commentedon May 16, 2019
Bruce Payette (@bpayette), so, this is expected to be usable:
And this is why an array should not be the key of a hashtable.
Item()obviously solves the problem, but ONLY if you know that the key is an array. If you were passed both the key and the hash, you may not be fully away of what methods you can use.vexx32 commentedon May 16, 2019
Tbh, I generally tend to expect that the hashtable literal syntax wouldn't accept anything other than a string as a key name, and am then generally surprised when it does strange things like this.
I almost want to say it's worth breaking some of the more esoteric scripts and restricting the literal syntax to only use value types as keys; after all, using a reference type as a key is... gonna be weird at the best of times. You need a reference to that item to get the item back from the table, at which point it almost becomes pointless to keep the data in a hashtable anyway.
bpayette commentedon May 16, 2019
Carl Morris (@msftrncs) Yes - this is supposed to be usable.
Right. And if it's your code, then you should know that. If it's a 3rd party library, then you need to know how that library works in order to use it properly. I would recommend against doing this in a public interface because it is confusing.
Rain Sallow (/u/ta11ow) (@vexx32) Restricting the keys to non-mutable types (not just value types since strings are references) would have make sense but we didn't think about that way back in V1.
vexx32 commentedon May 16, 2019
Right, but considering it's still for most intents and purposes utterly arcane and unusable I think that would be a change worth making, no?
bpayette commentedon May 16, 2019
Rain Sallow (/u/ta11ow) (@vexx32) Yes it's arcane but clearly Dongbo Wang (@daxian-dbw) had a reason from opening this bug.
chuanjiao10 Anxiety is a good word :-)
vexx32 commentedon May 16, 2019
Indeed, Bruce Payette (@bpayette); it arose from this arguably even more arcane situation that one can find themselves in: #9564
msftrncs commentedon May 17, 2019
Just to clarify, that hashtable comes from .NET right? So the same class can be used in the same way from other .NET languages (though their syntax might be more conventional (key=object, value=object))? So while PowerShell could have syntactically prevented a non-mutable key, that would not have prevented someone in a C# cmdlet from producing one?
Rain Sallow (/u/ta11ow) (@vexx32), I think you mentioned the wrong person above. :)
vexx32 commentedon May 17, 2019
Oops, fixed.
Yes, I'm talking only about the PowerShell literal syntax. Even in PS, you could still opt to add weird keys to your hashtable via the
$ht.Add()method if you choose.I'm just thinking it probably makes the most sense for PS not to support that in its literal hashtable declarations due to the esoteric and confusing nature of how it works with objects that can only be compared by reference.
SeeminglyScience commentedon May 20, 2019
While I agree that it's difficult to find a use case for an array as a key, I don't agree as a general rule for all reference types. For example a reference type:
System.RuntimeType)GetHashCodeand the equality operators/methods (though, hopefully it would also be immutable in design)microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.