Redefining Identical Struct

Redefining a struct with an identical definition does not lead to an ERROR: LoadError: invalid redefinition of constant error:

mutable struct LFKernel
  A0::Array{Float64, 2}
end

mutable struct LFKernel
  A0::Array{Float64, 2}
end

Is it supposed to? After reorganizing some code, I found I had duplicate definitions in different files.

It also appears this can be used to trick the language into keeping the default constructor while also providing an inner constructor:

mutable struct LFKernel
  A0::Array{Float64, 2}

  function LFKernel(a::Int, b::Int)

    return new(zeros(a, b))
  end
end

mutable struct LFKernel
  A0::Array{Float64, 2}
end


LFKernel(2, 3)  # no error
LFKernel(zeros(2, 3))  # also no error

This could potentially lead to confusion about what constructors a type has by looking at the source code when the definitions are in different files.

I think this is indeed intended.

Is there a rationale for this? On the surface, it doesn’t look particularly useful.

including a file twice in the REPL for example.

1 Like