No Method matching: Constructors

I am having difficulties defining a module and testing it.
I have the following architecture:

file: my_struct.jl

mutable struct my_struct
    x
    y
    z
end

function my_struct(x, y)
   z = Vector(undef, x)
   return my_struct(x,y,z)
end

file my_module.jl

module my_module

export my_struct

include("my_struct.jl")

end

file test.jl

include("my_module.jl")
using .my_module

b = my_struct(x,y)

When running test.jl I get an error saying “No method matching my_struct(::type, ::type). Closest candidates are my_struct(::Any, ::Any, ::Any)”.
Can anybody explain why I can not use the self-built constructor in test.jl and how to fix this?

It works when I test it in my REPL…


julia> mutable struct my_struct
           x
           y
           z
       end

julia> my_struct(x,y) = my_struct(x,y,Vector(undef,x))
my_struct

julia> my_struct(1,2)
my_struct(1, 2, Any[#undef])
1 Like

You are right it works it the simplified example.
Could you explain how I can avoid the warnings:
“replacing module by my_module.”
“using my_module.my_struct in module Main conflicts with an existing identifier.”

Restarting the REPL would certainly fix that.

1 Like

Thank you!