StringComparison to a StringComparer, to avoid bunch of switch-statements in user code (see 'Motivation' section below).string.GetHashCode(StringComparison) API, to make it easier/more discoverable to get the culture-dependent, case-insensitive, etc. hash of a string. Currently, you have to do StringComparer.XXX.GetHashCode(str), which is verbose & hard to find.```c#
public class StringComparer
{
public static StringComparer FromComparison(StringComparison comparisonType);
}
public sealed class String
{
public int GetHashCode(StringComparison comparisonType);
}
### Motivation
If someone has a variable comparison they don't have to do a bunch of switch-cases (we'll do them instead):
```c#
public static StringComparer FromComparison(StringComparison comparisonType)
{
switch (comparisonType)
{
case StringComparison.Ordinal:
return StringComparer.Ordinal;
case StringComparison.OrdinalIgnoreCase:
return StringComparer.OrdinalIgnoreCase;
...
}
}
For more background: See @stephentoub's suggestion in https://github.com/dotnet/corefx/issues/8034#issuecomment-261519913
cc @justinvp @Elion
How is StringComparer.Ordinal.GetHashCode(str) more verbose than str.GetHashCode(StringComparison.Ordinal), when it's 2 characters shorter?
How is StringComparer.Ordinal.GetHashCode(str) more verbose than str.GetHashCode(StringComparison.Ordinal), when it's 2 characters shorter?
I think it's more about discoverability. Not everyone knows that you can use StringComparer to get a hash code. I've seen a bunch of uses of text.ToLowerInvariant().GetHashCode() when StringComparer.OrdinalIgnoreCase.GetHashCode(text) would have been better.
To be honest, I do know that you can use StringComparer that way, and I still forget sometimes and go through a case transform.
@svick Another thing to mention is that str.GetHashCode(StringComparison.Ordinal) has less dots, so you have to hit Enter less in VS.
@jamesqo I count 2 dots in StringComparer.Ordinal.GetHashCode(str) and 2 dots in str.GetHashCode(StringComparison.Ordinal). 馃槈
@svick Sorry, my logic was faulty yesterday :smile: I was trying to say that, with an instance method, once you type GetHashCode( VS will detect that the method accepts an enum & will offer autocomplete for StringComparison. Whereas, with the current scenario, you need to type at least up to StringC before VS guesses StringComparer, otherwise you need to move the selection down.
Anyway, @justinvp's argument is the more important one. Regardless if one version is 2 characters shorter or has better completion, it needs to be easier for a newbie to discover how to do these things by looking at the methods on String, instead of scouring the whole System namespace.
Is there any reason that it shouldn't be better to encourage people to be hanging onto IEqualityComparer<string> in the first place rather than StringComparison? Their code is more open for extension that way.
@jnm2 I see your point. If we accepted an IEqualityComparer<string>, however, it would not help with discoverability since people would have no way of knowing to plug in a StringComparer.
@jamesqo <see cref="StringComparer"/>?
@jnm2 An XML doc comment does not help much-- some people might not read it. Besides, if you already have a StringComparer you can do _yourComparer.GetHashCode(str) whereas that is not the case with StringComparison (with this proposal, you'd need to write StringComparer.FromComparison(_comparison).GetHashCode(str) which is kinda verbose).
@jamesqo I think this makes sense. Marking ready for review.
FYI: Top post reformatted to match "formal API proposal" form - it is easier to read and comprehend in reviews.
Looks good as proposed.
@karelz I am working on this issue.
We still need tests
The PR dotnet/corefx#14623 seems to be abandoned. Unassinging the issue - it is "up for grabs" again, available for anyone to pick it up and finish it.
@MikevanDongen I have sent you contributor invite - once you accept, we can assign this issue to you. Please ping me here when you accept.
In future, please ping issues when you grab them, so that others know as well and we can assign them to you. Thank you!
@karelz @MikevanDongen this issue also tracks exposing String.GetHashCode(StringComparison comparisonType); from CoreCLR, which I see was implemented. @MikevanDongen do you plan to put up another PR to expose that one?
/cc @stephentoub re unexposed API.
I don't mind doing so (later this week).
-------- Original Message --------
From: Dan Moseley notifications@github.com
Sent: Sunday, April 30, 2017 11:43 PM
To: dotnet/corefx corefx@noreply.github.com
Subject: Re: [dotnet/corefx] Proposal: Make it easier to use StringComparison & StringComparer with GetHashCode. (#13800)
CC: Mike van Dongen github@mikevandongen.nl,Mention mention@noreply.github.com
@karelz @MikevanDongen this issue also tracks exposing
String.GetHashCode(StringComparison comparisonType);from CoreCLR, which I see was implemented. @MikevanDongen do you plan to put up another PR to expose that one?/cc @stephentoub re unexposed API.
--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
https://github.com/dotnet/corefx/issues/13800#issuecomment-298259227
@MikevanDongen this API is approved to go in so long as the PR is committed before 5/10 when we fork off. After that it would need shiproom ok which might not happen. Just let us know if you don't think you'll be able to get to it soon enough and maybe we can find someone. Thanks :)
@karelz
Let's keep it unassigned, until someone grabs that up. Whoever picks it up, please comment here .... first commenter wins the issue ;-)
Maybe @hughbe would like the honor of creating a method on String...
What's left to do, add String.GetHashCode to src\System.Runtime\ref\System.Runtime.cs and put tests on it in src\System.Runtime\tests\System\StringTests.netcoreapp.cs?
I can't find StringComparer in src\System.Runtime\ref\System.Runtime.cs. Where is that exposed?
Almost done writing tests, so I guess I'll take this. :-p
Thanks @jnm2! Assigned to you ...
Thanks for taking this on! Although dan suggested I give it a go I wouldn't be able to find time before ZBB so it's awesome to see
Most helpful comment
I think it's more about discoverability. Not everyone knows that you can use
StringComparerto get a hash code. I've seen a bunch of uses oftext.ToLowerInvariant().GetHashCode()whenStringComparer.OrdinalIgnoreCase.GetHashCode(text)would have been better.