Constructor compilation error

I have this very naive custom data structure and an associated constructor:

module MyModule

struct MyStruct{TG,TF<:AbstractFloat}
    g::TG
    Δt::TF
end

function MyStruct(g::TG, Δt::TF) where {TG, TF<:AbstractFloat}
    return MyStruct(g, Δt)
end

end

and when I try to use it:

using MyModule

I get the following errors:

WARNING: Method definition (::Type{MyModule.MyStruct{TG, TF} where TF<:AbstractFloat where TG})(TG, TF<:AbstractFloat) where {TG, TF<:AbstractFloat} in module MyModule at /Users/grs53/code/scratch/MyModule.jl:4 overwritten at /Users/grs53/code/scratch/MyModule.jl:8.
  ** incremental compilation may be fatally broken for this module **

I know this constructor does essentially nothing – is that the source of the problem? How should I handle such a case?

1 Like

I’m not 100% on this but it looks to me like your outer constructor is exactly the same as the default constructor which is created when you define the struct. I guess rather than asking how to handle this I would ask why are you doing it?

1 Like

As pointed above, the function is overwriting the default constructor. But note also that it becomes self-referential and a call to it may well end in a stack overflow.

(If you need a constructor with the same parameters as the default constructor you need to define an inner constructor, take a look at the manual section below)

https://docs.julialang.org/en/v1/manual/constructors/#man-inner-constructor-methods

1 Like

Because I thought I needed a separate constructor even in this trivial case :expressionless:

1 Like