-
-
Notifications
You must be signed in to change notification settings - Fork 706
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
Fix issue 17190: Fix isNumeric conflict. #5763
Conversation
|
Thanks for your pull request, @jmdavis! Bugzilla references
|
std/string.d
Outdated
| +/ | ||
| alias isNumeric = isNumber; | ||
|
|
||
| unittest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to Circle CI:
std/string.d(5814:1)[warn]: A unittest should be annotated with at least
@safeor@system
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. That should be fixed now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, though leaving the decision to @andralex
Templatizing std.string.isNumeric made it conflict with std.traits.isNumeric and does not overload well given that one is a function, and the other is an eponymous template. This renames std.string.isNumeric to isNumber and adds an alias named isNumeric that we will deprecate after it's been out a release. That way we can move away from having std.traits and std.string conflict so badly with regards to isNumeric.
|
This rename does not make me happy. I think it would be less disruptive to rename the |
Never mind that. I'm seeing a lot of hits for both in https://github.com/search?l=&q=isNumeric++language%3AD&ref=advsearch&type=Code&utf8=%E2%9C%93, though the std.traits template seems more common. Still, there are too many uses of either. We already have a similar issue with |
|
std.string.isNumeric appears to only be used in one place in all of Phobos outside of std.string, and that's in std.socket. I have no idea how much either is used in the wild. Personally, I'm sure that I've never used std.string.isNumeric, because any case where I might use it, I'd either just use std.conv.to and let it throw on failure, or I'd be looking for it specifically to be all digits, and I'd just be using something like all!isDigit. I don't recall whether I've ever had a need for std.traits.isNumeric. But std.traits.isNumeric and std.string.isNumeric do distinctly different things and are used in completely different ways and yet they end up conflicting. In general, I think that it's an incredibly bad idea to have conflicts when one is a function and the other is an eponymous template. Under other circumstances, they wouldn't conflict, because they aren't used the same way, but because one's an eponymous template and the other is a templated function, we end up with a conflict. I think that it's far better that we just rename one of them. Now, if Andrei wants to decide that this isn't worth worrying about, then he can close the PR and the bug, but I think that in this case, we're better off not having a conflict. As for the name, I think that using isNumeric for checking the type makes more sense than for checking the value of a string, and whereas isNumber works for the checking the string, I don't think that it makes anywhere near as much sense for checking the type. So, if one of them is going to be renamed to isNumber, I think that it should be the one in std.string. But if someone has a better name, fine. I'd prefer not to bikeshed it though, and I really don't see a problem with this particular renaming choice if we're going to rename one of them. The first question though is whether Andrei agrees with renaming either, since if he doesn't, then the whole issue is moot. |
|
First off, the claim is overdone. This is not a conflict. The same name can and will exist in D in different modules comfortably. This is not C, TeX, etc. where people need to make sure the names in their APIs are unique in the world. We should allow such names to coexist in the standard library. Second, So if I'd approve anything at all it's not going to be a witch hunt for identical names in different modules, though I do sympathize with the loss of comfort at the call site. I would approve deprecating |
|
It does make some sense to check whether something is a valid number before converting it given how expensive exceptions can be, but in that case, an API that returned a I have no problem with deprecating Thinking about this, I suspect that the best thing to do overall would be to add something to std.conv that was similar to |
I like this direction - offering APIs that don't throw, but leave the explicit decision to user code whether he uses
FWIW other languages have the concept of e.g.
An example from Elm - note that String.toInt "5" |> Result.toMaybe |> Maybe.withDefault 0I'm not sure yet what the best API approach in D for this would be, but out of my gut it's probably the reverse: let the main API return a |
|
+1 I occasionally miss expected<T, Error> from https://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C |
|
@jmdavis Looks like this can be closed then? |
|
So this discussion doesn't go to naught, how about putting this function on the way to obsolescence? Marginalize in the documentation with a recommendation for alternative approaches using |
Templatizing std.string.isNumeric made it conflict with
std.traits.isNumeric and does not overload well given that one is a
function, and the other is an eponymous template.
This renames std.string.isNumeric to isNumber and adds an alias named
isNumeric that we will deprecate after it's been out a release. That way
we can move away from having std.traits and std.string conflict so badly
with regards to isNumeric.
This doesn't outright fix the regression, since we're still stuck with the conflict for a while, but I think that it's the cleanest way to deal with the problem.
I'm adding the @andralex label since renaming a symbol like this is essentially adding a new symbol.