# Defining my own \`next!\` but avoiding confict

**URL:** <https://discourse.julialang.org/t/defining-my-own-next-but-avoiding-confict/95035>\
**Category:** General Usage\
**Created:** [February 22, 2023, 5:55pm UTC](https://discourse.julialang.org/t/defining-my-own-next-but-avoiding-confict/95035 "2023-02-22T17:55:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scheinerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scheinerman/32/35303_2.png) [@scheinerman](https://discourse.julialang.org/u/scheinerman)\
**Post date:** [February 22, 2023, 5:55pm UTC](https://discourse.julialang.org/t/defining-my-own-next-but-avoiding-confict/95035/1 "2023-02-22T17:55:49Z")

</div>

I would like to create a `next!` function for a custom type, but avoid conflict with other modules that might define `next!` as well. A very small example:

```julia
module NextTest

export Numb, next!

mutable struct Numb
    val::Int
    function Numb()
        new(0)
    end
end

function next!(n::Numb)
    n.val += 1
    return n.val 
end

end # module NextTest

```

I start using it like this:

```julia
julia> using NextTest

julia> n = Numb()
Numb(0)

julia> next!(n)
1

julia> next!(n)
2

```

And now I want to use `ProgressMeter` which also defines `next!` and I get this:

```julia
julia> using ProgressMeter
WARNING: using ProgressMeter.next! in module Main conflicts with an existing identifier.

```

I would have thought that multiple dispatch would allow the two `next!` functions to easily co-exist since they operate on different types.

I realize that I could add this line to my module’s definition:

```julia
import ProgressMeter: next!

```

and the problem goes away. But there may be other modules that want to define `next!` and it’s not clear why (say) `ProgressMeter` should be the “owner” of this function name.

Is there another approach that would be better?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 22, 2023, 6:47pm UTC](https://discourse.julialang.org/t/defining-my-own-next-but-avoiding-confict/95035/2 "2023-02-22T18:47:20Z")

</div>

None of the packages “own” the function. If you are extending the function from ProgressMeter it is natural to import it in your module. If you are not extending it, then the two next functions are different functions, and having to use them with the explicit module name is necessary to avoid conflicts.

Note that, of course, you can define any (non-base) function name inside your module, acting on types that you _do not_ own. If that automatically was considered adding methods to functions with common names it would be impossible to prevent type piracy.

Ps. The specific problem is avoided by using `import ProgressMeter` instead of `using`, such that you do not bring it’s functions to your name space.

---

<div class="post-metadata">

**Author:** ![scheinerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scheinerman/32/35303_2.png) [@scheinerman](https://discourse.julialang.org/u/scheinerman)\
**Post date:** [February 22, 2023, 7:14pm UTC](https://discourse.julialang.org/t/defining-my-own-next-but-avoiding-confict/95035/3 "2023-02-22T19:14:15Z")

</div>

Thanks. I just figured that since “my” `next!` is defined for different types than other functions named `next!` , that would be suffice to disambiguate and I would not have to write `My.next!` vs `Their.next!`.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [February 22, 2023, 7:22pm UTC](https://discourse.julialang.org/t/defining-my-own-next-but-avoiding-confict/95035/4 "2023-02-22T19:22:36Z")

</div>

Do you actually need to use `ProgressMeter.next!` in your code? If, for example, you only need `@showprogress`, then I would do the following:

```julia
julia> next!() = 1
next! (generic function with 1 method)

julia> using ProgressMeter: @showprogress

```

Note that this does not generate a warning. If you do need `ProgressMeter.next!`, then as @lmiq mentioned, you can do `import ProgressMeter` and then write `ProgressMeter.next!` whenever you need the `next!` from ProgressMeter. Note that when you do this you won’t need to prefix your own version of `next!`.

Also note that you can combine `import` and `using`, so that you can use other names from ProgressMeter without prefixing them:

```julia
julia> next!() = 1
next! (generic function with 1 method)

julia> using ProgressMeter: @showprogress

julia> import ProgressMeter

julia> ProgressMeter.next! === next!
false

```

---

<div class="post-metadata">

**Author:** ![scheinerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scheinerman/32/35303_2.png) [@scheinerman](https://discourse.julialang.org/u/scheinerman)\
**Post date:** [February 22, 2023, 7:35pm UTC](https://discourse.julialang.org/t/defining-my-own-next-but-avoiding-confict/95035/5 "2023-02-22T19:35:29Z")

</div>

Thanks. I don’t actually need `ProgressMeter` in the case I’m considering. But _sometimes_ I would use it together with my own project. In any case, I can use the long-form names in the instances when I do use both. I was just curious about why this was even necessary since the two `next!` functions take different types of arguments. OK to let this go. Thanks so much!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 22, 2023, 7:36pm UTC](https://discourse.julialang.org/t/defining-my-own-next-but-avoiding-confict/95035/6 "2023-02-22T19:36:19Z")

</div>

Sort off, what if you wanted your code to error if your `next` was called with the wrong type?

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [February 22, 2023, 7:51pm UTC](https://discourse.julialang.org/t/defining-my-own-next-but-avoiding-confict/95035/7 "2023-02-22T19:51:10Z")

</div>

> [@scheinerman](#):
>
> I was just curious about why this was even necessary since the two `next!` functions take different types of arguments.

There is a very long thread on Discourse about this very subject, but I don’t have a link handy. Basically, the philosophy is that a generic function should have a generic definition that applies to all its methods. In this case, `ProgressMeter.next!` and your `next!` have very different meanings, so they should not be methods of the same generic function.

EDIT: Found the link:

> [@Function name conflict: ADL / function merging?](https://discourse.julialang.org/t/function-name-conflict-adl-function-merging/10335):
>
> Is there any reason why Julia needs me to fully qualify the function name when the argument types are clear and there shouldn’t be any problem with multiple dispatch? Case 1: load JuliaDB before defining Foo julia\> using JuliaDB julia\> stack( stack(t::D) where D\<:Union{IndexedTables.NDSparse, IndexedTables.NextTable} in IndexedTables at /Users/tomkwong/.julia/v0.6/IndexedTables/src/reshape.jl:32 stack(t::D, by; select, variable, value) where D\<:Union{IndexedTables.NDSparse, IndexedTables.Next…
