# Cannot find overloaded method

**URL:** https://discourse.julialang.org/t/cannot-find-overloaded-method/48633
**Category:** General Usage
**Created:** [October 19, 2020, 2:36pm UTC](https://discourse.julialang.org/t/cannot-find-overloaded-method/48633 "2020-10-19T14:36:18Z")
**Posts on this page:** 9
**Page:** 2

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [October 19, 2020, 8:17pm UTC](https://discourse.julialang.org/t/cannot-find-overloaded-method/48633/22 "2020-10-19T20:17:28Z")

</div>

> [@Henrique\_Becker](#):
>
> No, it is not. Implementation is not documentation of intention.

The OP had a simple question: -Why doesn’t this implementation work?

You are _really_ taking this ball and running with it. If you don’t want to take the OP at face value, maybe ask what the intention was.

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [October 19, 2020, 8:33pm UTC](https://discourse.julialang.org/t/cannot-find-overloaded-method/48633/23 "2020-10-19T20:33:19Z")

</div>

Wow, thanks a lot for the numerous replies! I marked a solution, which is the code that actually fixes the MethodError in my original post without changing behaviour. Unfortunately, it’s not possible to mark several solutions, but many comments were useful for the understanding.

Now I see that indeed a more specific `replace` method is defined in (surprisingly!) `set.jl` in base julia. It explicitly throws a methoderror, and that’s why the resulting error message is extremely confusing: says that the method is not found, and lists this very method below.

As for the desired behaviour and replacement order - it would be best if the replacements were “simultaneous” as described in this thread above. My implementation makes replacements one-by-one just because it is the simplest possible implementation, and in actual usage I typically don’t care about the order: replacements are not ambigous. It’s just really convenient to write `replace(str, a => b, c => d, e => f, g => h)` - compare to `replace(replace(replace(replace(str), a => b), c => d), e => f), g => h)`.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [October 19, 2020, 8:52pm UTC](https://discourse.julialang.org/t/cannot-find-overloaded-method/48633/24 "2020-10-19T20:52:18Z")

</div>

This is an alternative. A few more characters but no piracy.

```julia
foldl(replace, [str, a => b, c => d, e => f, g => h])

```

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [October 19, 2020, 9:17pm UTC](https://discourse.julialang.org/t/cannot-find-overloaded-method/48633/25 "2020-10-19T21:17:15Z")

</div>

That’s indeed a shorter version - so the definition with `for` loop can be replaced with:

```julia
Base.replace(str::AbstractString, old_new1::Pair, old_new::Pair{String, String}...) = foldl(replace, [str, old_new1, old_new...])

```

But I would still find `replace(x, a => b, c => d, ...)` better at least for interactive usage:

- Extending a single replacement `replace(x, a => b)` with the second one is easy: just add `, c => d`.
- Intent is clear from the code: perform multiple replacements. No need to remember the fold-replace-str combination and order.

I don’t see anything wrong in type piracy per se. E.g., I’ve added the `first(vec, n)` and `last(vec, n)` methods in my personal utilities package more than a year ago and been using them just fine. When julia 1.6 will get released (with the [corresponding PR](https://github.com/JuliaLang/julia/pull/34868)) I will just remove those two methods from my package and that’s it. And there are many more cases, e.g. `deg2rad(::Missing)`, `lowercase(s::Symbol)`, etc - everything works fine, I had no issues with those.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 20, 2020, 8:32am UTC](https://discourse.julialang.org/t/cannot-find-overloaded-method/48633/26 "2020-10-20T08:32:13Z")

</div>

> [@aplavin](#):
>
> I don’t see anything wrong in type piracy per se. […] I will just remove those two methods from my package and that’s it.

In your personal utility package you are of course free to do what you like, but it is still bad style in packages intended for general use.

Because of this, I think it is best if Julia programmers just stay away from type piracy (unless there is a compelling reason) — it is just easier to make this a habit in general and not think about it any more.

---

<div class="post-metadata">

### Author: ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)
#### Post date: [October 20, 2020, 8:42am UTC](https://discourse.julialang.org/t/cannot-find-overloaded-method/48633/27 "2020-10-20T08:42:56Z")

</div>

It seems to me many extensions of methods/ new methods which found their way in base started  
with extensions in packages — what you call piracy. So you better find the right equilibrium between stifling progress by forbidding all piracy and sowing chaos with widespread piracy.

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [October 20, 2020, 8:46am UTC](https://discourse.julialang.org/t/cannot-find-overloaded-method/48633/28 "2020-10-20T08:46:42Z")

</div>

I fully agree with you that piracy should be kept in personal packages or packages that explicitly state that they perform said piracy _(automatic detection possible?)_. But to completely stay away from it seems too strict for me.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 20, 2020, 8:57am UTC](https://discourse.julialang.org/t/cannot-find-overloaded-method/48633/29 "2020-10-20T08:57:16Z")

</div>

> [@Jean\_Michel](#):
>
> forbidding all piracy

I think you misunderstand how the Julia package ecosystem works. Currently, the General registry does not forbid type piracy, it is just a style recommendation. You are still free to do it. There is no type piracy police.

> [@Jean\_Michel](#):
>
> many extensions of methods/ new methods which found their way in base started  
> with extensions in packages — what you call piracy

I am not sure what you mean here, but in any case extensions rarely need to pirate `Base` methods. Packages that really need to do type piracy are usually very explicit about it to warn users, eg

> **[GitHub - JuliaSIMD/SLEEFPirates.jl: Pirated SLEEF port.](https://github.com/JuliaSIMD/SLEEFPirates.jl)**
>
> Pirated SLEEF port. Contribute to JuliaSIMD/SLEEFPirates.jl development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [June 12, 2021, 9:43pm UTC](https://discourse.julialang.org/t/cannot-find-overloaded-method/48633/30 "2021-06-12T21:43:23Z")

</div>

Just saw it in the Julia 1.7 news:

> `replace(::String)` now accepts multiple patterns, which will be applied left-to-right simultaneously

This means there will be no need for the `replace` method discussed in this thread, it will just work with `Base`!

```julia
julia> replace("abc", "a" => "x", "b" => "y")
"xyc"

```

(:

[Previous page](https://discourse.julialang.org/t/cannot-find-overloaded-method/48633.md?page=1)
