# Is there a way to know in advance if a function definition will overwrite a previous one?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-know-in-advance-if-a-function-definition-will-overwrite-a-previous-one/114670>\
**Category:** General Usage\
**Tags:** question, function\
**Created:** [May 24, 2024, 9:42am UTC](https://discourse.julialang.org/t/is-there-a-way-to-know-in-advance-if-a-function-definition-will-overwrite-a-previous-one/114670 "2024-05-24T09:42:00Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [May 25, 2024, 11:34am UTC](https://discourse.julialang.org/t/is-there-a-way-to-know-in-advance-if-a-function-definition-will-overwrite-a-previous-one/114670/21 "2024-05-25T11:34:24Z")

</div>

But if I read it correctly 5. is saying that I can read from a global const, which is something we can easily do e.g. in your example I can put `const BREAKPOINTS = []`

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [May 25, 2024, 11:49am UTC](https://discourse.julialang.org/t/is-there-a-way-to-know-in-advance-if-a-function-definition-will-overwrite-a-previous-one/114670/22 "2024-05-25T11:49:02Z")

</div>

I tried your code in a module, calling `foo` doesn’t print anything ☹

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [May 25, 2024, 11:55am UTC](https://discourse.julialang.org/t/is-there-a-way-to-know-in-advance-if-a-function-definition-will-overwrite-a-previous-one/114670/23 "2024-05-25T11:55:00Z")

</div>

What did you try exactly? My code does not contain any prints so would not print anything 😅

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [May 25, 2024, 11:57am UTC](https://discourse.julialang.org/t/is-there-a-way-to-know-in-advance-if-a-function-definition-will-overwrite-a-previous-one/114670/24 "2024-05-25T11:57:41Z")

</div>

ah yeah sorry, just misinterpreted the output…anyway given we can annotate the container with `global const` do you think we respect the contract?

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [May 25, 2024, 12:02pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-know-in-advance-if-a-function-definition-will-overwrite-a-previous-one/114670/25 "2024-05-25T12:02:13Z")

</div>

No. It reads “non-constant **state** ” and a vector in a constant variable is still mutable state. I think the issue is exactly that the function won’t update/recompile if this mutable state changes and thus Julia does not want to make any promises on consistency. So if we take care of that, I think we might be able to make it work somewhat safely (i.e. the generated function could also set a flag, that it ran and if someone tries to change the dispatch later, we can throw an error).

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [May 25, 2024, 12:17pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-know-in-advance-if-a-function-definition-will-overwrite-a-previous-one/114670/26 "2024-05-25T12:17:19Z")

</div>

I found this old package [GitHub - toivoh/PatternDispatch.jl: Method dispatch based on pattern matching for Julia](https://github.com/toivoh/PatternDispatch.jl), it doesn’t work on Julia 1.x but since it’s basically the same thing I guess it could enlighten us maybe @abraemer

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [May 25, 2024, 2:09pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-know-in-advance-if-a-function-definition-will-overwrite-a-previous-one/114670/27 "2024-05-25T14:09:20Z")

</div>

Maybe I found another solution!

Consider this:

```julia
julia> module Foo
            module SubFoo
               const BREAKPOINTS = []
               function __init__ ()
                   @eval parentmodule(SubFoo) x = SubFoo.BREAKPOINTS[end]
               end
            end
            push!(SubFoo.BREAKPOINTS, 1)
            push!(SubFoo.BREAKPOINTS, 3)
            push!(SubFoo.BREAKPOINTS, 5)
            push!(SubFoo.BREAKPOINTS, 7)
       end
Main.Foo

julia> Foo.x
7

```

This means if I’m not mistaken at the first call of `@dispatch` I could generate automatically a module with an ` __init__ ()` function which evaluates all the methods in the book-keeping

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [May 25, 2024, 2:32pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-know-in-advance-if-a-function-definition-will-overwrite-a-previous-one/114670/28 "2024-05-25T14:32:29Z")

</div>

No I don’t think this would work in practice. AFAIK the ` __init__ ` is run as soon as you `import`/`using` the submodule. When would you do this in a package?  
I am not quite sure why this works in the REPL though.

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [May 25, 2024, 2:35pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-know-in-advance-if-a-function-definition-will-overwrite-a-previous-one/114670/29 "2024-05-25T14:35:42Z")

</div>

It should actually @abraemer, I tested the exact same code also in a package I dev, where precompilation happened before, same behaviour 🙂

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [May 25, 2024, 2:39pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-know-in-advance-if-a-function-definition-will-overwrite-a-previous-one/114670/30 "2024-05-25T14:39:33Z")

</div>

the docstring seems to confirm this:

```julia
  The __init__ () function in a module executes immediately after the module is loaded at runtime
  for the first time. It is called once, after all other statements in the module have been
  executed. Because it is called after fully importing the module, ** __init__ functions of
  submodules will be executed first**.

```

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [May 25, 2024, 3:00pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-know-in-advance-if-a-function-definition-will-overwrite-a-previous-one/114670/31 "2024-05-25T15:00:01Z")

</div>

Ah yes, I had read that but misinterpreted it. Then it could work! 👍

[Previous page](https://discourse.julialang.org/t/is-there-a-way-to-know-in-advance-if-a-function-definition-will-overwrite-a-previous-one/114670.md?page=1)
