-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
GroupCollection should implement IReadOnlyDictionary interface to align with its Dictionary-Type Usage #23186
Comments
@michael-hawker good suggestion, would you be willing to update to our standard format for review? https://github.com/dotnet/corefx/blob/master/Documentation/project-docs/api-review-process.md |
The sugar I'd prefer would be
|
@danmosemsft will do. @jnm2 I'll add that to my request as well though I think it'd be I did notice that currently the non-existence groups are empty strings and not null (at least in UWP), so it's a little ambiguous currently as to the result (i.e. if the group captured but contained nothing vs. didn't exist at all). |
@michael-hawker thanks for the suggestion! I will look into and will comment as soon as I fully reviewed it. |
Potential hackathon candidate if we review the API addition in time. cc @karelz |
Looks good.
|
For clarity, he meant "Given that we implement IDictionary<>, should we also implement IReadOnlyDictionary<>?". |
I also wanted to clarify:
Is this a separate bug and should be filed elsewhere or expected? |
@michael-hawker that sounds like separate issue to me. |
@karelz I didn't realize there was a
Output:
For example in the 2nd case above, I was expecting the It feels like this proposal would want to factor the Thoughts? I'm happy to open another issue to discuss this behavior, but it definitely seems related as if you use the |
I don't think that TryGetValue should also consider the Success value. That's not the notion of the ContainsKey or TryGetValue pattern. I expect consumers first check if success and only then call TryGetValue. |
i'll work on this one during the hackaton |
For some reason I'm finding it unintuitive. If I see a TryGetValue method in intellisense, I'm going to think it's primarily about .Success, and I'd be wrong. |
TryGetValue operates on collections that contain a key and a value, i.e. Hashtable and Dictionary. In our case the key is the group name and the value is the group object. If the key exists then the value should be returned without taking its internal state into consideration (in our case the Success property). You will get the Group object back, same as you would do GroupCollection["group1"] today. You still want to check the Success property in both scenarios. |
Oops, I was picturing TryGetValue on Group, lol! I'm sorry! |
@peltco sent you the invite - see https://github.com/dotnet/corefx/wiki/Hackathon#how-can-i-grab-an-issue |
Yeah, I think the crux of the issue is that groups will appear regardless of if they're captured (as long as they're defined in the RegEx string), so it's a bit of understanding the API. |
GroupCollection
acts like a dictionary, but doesn't provide all of the familiar methods found on Dictionary collections. This makes it harder to inspect contents to make informed decisions on its contents to drive logic. For instance, determining if a key was present in a named capture group before trying to perform logic with the result. This can be seen as a follow-on to issue #13933 (it was listed in the Open Questions section).Rationale and Usage
This was something I stumbled upon when trying to make a RegEx expression in two forms, one with a lot of named parameters, but that were all optional. I wanted to check if the parameters existed before proceeding to perform logic with them but discovered that there was no method to check if a named capture group was actually captured. Currently, I can use an indexer to access a specific group name like
But unlike a normal dictionary, I can't call
match.Groups.ContainsKey('options")
to see if it exists first before retrieval if I have optional named capture groups. The current Contains method only checks on the object not the named key.Currently, the group if not captured returns an empty string, but that is a bit misleading, as the group could have been captured but contained no data. So, there's an ambiguous state. If we had methods to check if the capture was performed, we could leave the group out and have KeyNotFoundExceptions or nulls to differentiate from existence vs. no data. This would let us then do:
OR
Proposed API
Details
Updates
The text was updated successfully, but these errors were encountered: