-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
deprecate replace(s::String, pat, r, n) to replace(s, pat=>r, count=n) #25165
Conversation
bb332b1
to
6aaf9f7
Compare
base/set.jl
Outdated
``` | ||
""" | ||
replace!(pred::Callable, A, new; count::Integer=typemax(Int)) = | ||
replace!(x -> if pred(x) Some(new) end, A, count=count) |
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.
Won't this be nothing
when pred(x)
is false
? That seems oddly implicit. IMO it would be clearer to write it as x->pred(x) ? Some(new) : nothing
if that is indeed the intent.
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.
I find lovely this implicit nothing
, but I appreciate many will disagree. I was wondering if this is a pattern which will emerge, to pune on the nothing
return value from if
without else
and for
. I remember that you precisely would have prefered something else than Void
to replace Nullable
... I'm absolutely fine to change this here, but will do next time there is a more serious reason to push (CI resources are scarces, though not as much as on deadline day ;-) )
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.
I don't think it matters much and it's kind of cute that this works, but I have to say I do prefer the explicitness of pred(x) ? Some(new) : nothing
. The if
version requires the reader to know that if
without and else
evaluates to nothing
when the condition is false.
If anywone wonders, the errors come from that I forgot to update stdlib code. I will update here if/when the other |
Why pairs and not keyword arguments? |
Because the LHS of a keyword argument can only be a symbol? |
It didn't occur to me, but my "feeling" is that pairs are much better suited for this. I see more keyword args as a way to tweak the algorithm implemented by the function rather that providing data. Or at least, the data would be the value of the the the kwarg, and the key part should be quite static, and documented. |
Oh nice, now I get it, thanks! Could this be a good opportunity to choose a more mnemonic name for the |
The name of the argument isn't really user-facing, so I don't see why it matters what's used. |
I don't agree, method signatures are user-facing and documentation too (when there are no docs all you get is the method signature). If you query for it's docstring this is the signature help?> replace("foofoofoo", r"foo", "bar", 2)
replace(s::AbstractString, pat, r, [count::Integer])
Search for the given pattern pat in s, and replace each occurrence with r. If count is provided, replace at most count occurrences. As with search, the second
argument may be a single character, a vector or a set of characters, a string, or a regular expression. If r is a function, each occurrence is replaced with
r(s) where s is the matched substring. If pat is a regular expression and r is a SubstitutionString, then capture group references in r are replaced with the
corresponding matched text. To remove instances of pat from string, set r to the empty String ("").
Here however we can see the docstring and the actual name of the argument are out of sync, here it is julia> @which replace("foofoofoo", r"foo", "bar", 2)
replace(s::AbstractString, pat, f, n::Integer) in Base at deprecated.jl:1345
julia> At least the docstring and the documented argument should be named the same. Some better wording would be great too IMHO, ie:
|
Positional argument names can be changed at any point without breaking anyone's code. We are at the moment focused on things that need to be changed for 1.0. We can bikeshed better positional argument names until the cows come home post 1.0. |
I see, I'll make an issue and open a PR for bad keyword argument names, since those would be breaking, hope there are none. :) |
Triage says please do this! |
6aaf9f7
to
6e17cb2
Compare
This is a follow-up of #22324, to have a consistent
replace
. This does not try to addreplace(s, pat1=>r1, pat2=>r2, ...)
as this can be added any time later.EDIT: this is based on that PR, so only one new commit here!