Conflicting import of function with different signatures from multiple modules

#1

I am having difficulty importing functions that have the same name but different signatures from two different modules. It seems to me that I should be able to import both functions, since the signatures are different, but the REPL yells at me. How can I do this?

As a MWE:

module TypeDef
    export VariableType
    abstract type VariableType end
end

module Mod1 
    using ..TypeDef
    export Foo, TestFcn

    mutable struct Foo <: VariableType
        x::Float64
        y::Float64

        function Foo(s::String)
            println(string)
            return new(x,y)
        end
    end

    function TestFcn(f::Foo, z::Float64)
        println("Variables: ", "$(f.x+z)")
    end

end

module Mod2
    using ..TypeDef
    export Bar, TestFcn

    mutable struct Bar <: VariableType
        x::Float64
        y::Float64

        function Bar(s::String)
            println(string)
            return new(x,y)
        end
    end

    function TestFcn(f::Bar, z::Float64)
        println("More variables: ", "$(f.y + z)")
    end
end

using .TypeDef

import .Mod1: Foo, TestFcn
import .Mod2: Bar, TestFcn

This returns the following:

julia> using .TypeDef
julia> import .Mod1: Foo, TestFcn
julia> import .Mod2: Bar, TestFcn
WARNING: ignoring conflicting import of Mod2.TestFcn into Main
0 Likes

#2

They are different functions, so you cannot “merge” them. This has been discussed at length.

You can either

  1. define TestFcn in TypeDef, and have both Mod1 and Mod2 import it (which I guess is what you want),
  2. import Mod1 and use Mod1.TestFcn, or not export it and do the same.

Also, note that most people use lowercase function names in Julia.

2 Likes