# Overwritten method warning during precompilation

**URL:** <https://discourse.julialang.org/t/overwritten-method-warning-during-precompilation/90603>\
**Category:** General Usage\
**Created:** [November 21, 2022, 8:20pm UTC](https://discourse.julialang.org/t/overwritten-method-warning-during-precompilation/90603 "2022-11-21T20:20:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [November 21, 2022, 8:20pm UTC](https://discourse.julialang.org/t/overwritten-method-warning-during-precompilation/90603/1 "2022-11-21T20:20:39Z")

</div>

Here is a simplified example demonstrating my problem. I have a package named `MyPkg2`:

```julia
module MyPkg2 # defined in MyPkg2.jl

using StaticArrays

const SBool{K} = SVector{K,Bool}
SBool(x::Number...) = SVector(Bool.(x)...)

# other useful functions are also defined but omitted

end # module

```

It imports `StaticArrays`, and because it uses `SVector` for `Bool` often, it defines a type alias `SBool{K} = SVector{K,Bool}` for convenience. Additionally, it defines `SBool(x::Number...)`, which conveniently creates `SBool{K}` when numbers convertible to `Bool` are passed; for example, `SBool(0,1,0)` generates `[false, true, false]` typed `SVector{3,Bool}`.

`MyPkg2` has other useful functions as well. To use those functions, I import `MyPkg2` in another package `MyPkg3`. However, because `MyPkg3` also uses `SVector` for `Bool` often, I define the type alias `SBool` in `MyPkg3` again. Here is how the package looks:

```julia
module MyPkg3 # defined in MyPkg3.jl

using MyPkg2
using StaticArrays

const SBool{K} = SVector{K,Bool}
SBool(x::Number...) = SVector(Bool.(x)...)

end # module

```

Because `SBool` in `MyPkg2` and `SBool` in `MyPkg3` live in different namespaces, I thought repeating its definition in both packages would be fine. However, when I use `MyPkg3`, I get the following warning:

```julia-repl
julia> using MyPkg3
[Info: Precompiling MyPkg3 [73b3b46d-e1cd-46a7-992c-0e3a8322463c]
WARNING: Method definition (::Type{StaticArrays.SArray{Tuple{K}, Bool, 1, K} where K})(Number...) in module MyPkg2 at /Users/wsshin/Programming/MyPkg2/src/MyPkg2.jl:6 overwritten in module MyPkg3 at /Users/wsshin/Programming/MyPkg3/src/MyPkg3.jl:7.
  **incremental compilation may be fatally broken for this module**

```

In my actual full-blown packages, the code performance has drastically reduced since the code was updated to produce this issue, probably because of the precompilation failure. Is there any way to eliminate this overwritten method warning?

Here is a more simplified example demonstrating the same issue without using `StaticArrays`, from which the solution to the issue might be more easily figured out:

```julia
## MyPkg1.jl
module MyPkg1 # instead of StaticArrays

export MyType

struct MyType end # instead of SVector

end

## MyPkg2.jl
module MyPkg2

using MyPkg1 # instead of StaticArrays

const MyTypeAlias = MyType # instead of SBool
MyTypeAlias(x) = x

end

## MyPkg3.jl
module MyPkg3

using MyPkg2
using MyPkg1 # instead of StaticArrays

const MyTypeAlias = MyType # instead of SBool
MyTypeAlias(x) = x

end

```

And here is the corresponding overwritten method warning during precompilation:

```julia-repl
julia> using MyPkg3
[Info: Precompiling MyPkg3 [73b3b46d-e1cd-46a7-992c-0e3a8322463c]
WARNING: Method definition (::Type{MyPkg1.MyType})(Any) in module MyPkg2 at /Users/wsshin/Programming/MyPkg2/src/MyPkg2.jl:6 overwritten in module MyPkg3 at /Users/wsshin/Programming/MyPkg3/src/MyPkg3.jl:7.
  **incremental compilation may be fatally broken for this module**

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 21, 2022, 9:02pm UTC](https://discourse.julialang.org/t/overwritten-method-warning-during-precompilation/90603/2 "2022-11-21T21:02:45Z")

</div>

> [@wsshin](#):
>
> Is there any way to eliminate this overwritten method warning?

No; you’re defining the same constant alias twice, after which you define the same methods for that original `SArray` twice. Whichever is defined later overwrites the first one (which is also type piracy, since you own neither `Bool` nor `SArray`).

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [November 21, 2022, 10:33pm UTC](https://discourse.julialang.org/t/overwritten-method-warning-during-precompilation/90603/3 "2022-11-21T22:33:05Z")

</div>

Just to be clear. Since you’re defining a method for an alias, you’re actually defining a method (a constructor) for `SVector`, i.e. for `SArray`, which lives in `StaticArrays`. So it is not in your module, but as Sukera says, type piracy (neither method nor any argument types are yours). You could, I think, keep the alias, but rather define an ordinary method:

```julia
sbool(x::Number...) = SVector(Bool.(x)...)

```

The `sbool` method will be local to your module. You then use `sbool` to create vectors, and you can use `SBool` in method signatures for inference.

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [November 21, 2022, 10:40pm UTC](https://discourse.julialang.org/t/overwritten-method-warning-during-precompilation/90603/4 "2022-11-21T22:40:43Z")

</div>

> [@wsshin](#):
>
> Here is a more simplified example demonstrating the same issue without using `StaticArrays`

If the module defining `MyType` is actually yours, you should rather define the alias and the simplified constructor in that module.
