Julia: Unexpected behaviour of `replace(::String...)`

Created on 4 Jan 2018  路  2Comments  路  Source: JuliaLang/julia

julia> replace("abc")
ERROR: MethodError: no method matching copy(::String)

Stacktrace:
 [1] #replace#247(::Int64, ::Function, ::String) at ./set.jl:667
 [2] replace(::String) at ./set.jl:667
 [3] top-level scope
  1. I would expect here a no-op, return the input string as output.
  2. Why don't we have copy(a::AbstractString) = a ?
julia> replace("abc", 'a'=>'b')
"bbc"

julia> replace("abc", 'a'=>'A', 'c'=>'C')
ERROR: MethodError: no method matching replace(::String, ::Pair{Char,Char}, ::Pair{Char,Char})
Closest candidates are:
  replace(::AbstractString, ::Pair, ::Pair) at set.jl:731
Stacktrace:
 [1] replace(::String, ::Pair{Char,Char}, ::Pair{Char,Char}) at ./set.jl:731
 [2] top-level scope
  1. That is confusing. At one hand "no method", but in the stacktrace exactly the required signature.
  2. It would be logical to implement replace(::AbstractString, ::Pair...) consistently - that would be in line with replace(::Set, ::Pair...)`
julia> replace("abc", 'a'=>'A', 'c'=>'C', 'b'=>'B')
ERROR: MethodError: no method matching copy(::String)
Closest candidates are:
  copy(::Expr) at expr.jl:35
  copy(::ObjectIdDict) at abstractdict.jl:614
  copy(::BitSet) at bitset.jl:38
  ...
Stacktrace:
 [1] #replace#247(::Int64, ::Function, ::String, ::Pair{Char,Char}, ::Vararg{Pair{Char,Char},N} where N) at ./set.jl:667
 [2] replace(::String, ::Pair{Char,Char}, ::Pair{Char,Char}, ::Pair{Char,Char}) at ./set.jl:667

Most helpful comment

@rfourquet replacing multiple pairs is a not-yet-implemented but of course would-be-nice-to-have.

it would be a honour to me to make such a PR.

All 2 comments

Thanks for reporting.

I would expect here a no-op, return the input string as output.

That makes some sense. The replace(::AbstractString, ::Pair) was added recently to match the new replace(::Union{Set,...}...) API, but only what was already implemented for strings has been replaced, i.e. replacing multiple pairs is a not-yet-implemented but of course would-be-nice-to-have.

That is confusing. At one hand "no method", but in the stacktrace exactly the required signature.

This is a bit tricky, as those "implemented" methods which throw MethodError are somehow necessary to resolve ambiguitites with other methods. Maybe throwing another type of error would reduce the confusion, but it's also true that those method don't really exist in a way that can be used.

@rfourquet replacing multiple pairs is a not-yet-implemented but of course would-be-nice-to-have.

it would be a honour to me to make such a PR.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

helgee picture helgee  路  3Comments

wilburtownsend picture wilburtownsend  路  3Comments

TotalVerb picture TotalVerb  路  3Comments

musm picture musm  路  3Comments

StefanKarpinski picture StefanKarpinski  路  3Comments