At least for interactive usage it is often convenient to have a replace
method for strings that can perform multiple replacements at once. So I try do define it, but julia (1.5) doesn’t find the new method even though it gets shown in the MethodError
:
julia> function Base.replace(str::AbstractString, old_new::Pair...)
for o_n in old_new
str = replace(str, o_n)
end
return str
end
julia> replace("abc", "a" => "x", "b" => "y")
ERROR: MethodError: no method matching replace(::String, ::Pair{String,String}, ::Pair{String,String})
Closest candidates are:
...
replace(::AbstractString, ::Pair...) at REPL[2]:1
...
Even stranger, everything works as expected if I replace AbstractString
with String
in the method definition.
Any idea what goes wrong? Is it a bug in Julia?
I know this is type piracy, but see nothing wrong in it for interactive usage.