Strange behavior of `struct` definition in REPL

This does not seem correct to me … Tried anything, also using a development module.
I’m using Julia-1.10.0.beta2 or Version 1.9.2
Same results. I’m in a deadlock !!
Any clue?

help?> Main.HPC
  No documentation found.

  Binding HPC does not exist.

julia> HPC = mutable struct HPC
           d::Int # intrinsic dimension
           m::Int # embedding dimension (rows of V)
           n::Int # number of vertices  (columns of V)
           V::Matrix{Float64} # object geometry
           C::Dict{Symbol, AbstractArray} # object topology (C for cells)
           # inner constructors
           HPC() = new( NaN, NaN, NaN, Matrix{Float64}, Dict{Symbol, AbstractArray} )
           HPC(m,n) = new( m,m,n, Matrix(undef,m,n), Dict{Symbol,AbstractArray} )
           HPC(d,m,n) = begin V = Matrix(undef,m,n), C = Dict{Symbol,AbstractArray}(); 
                     new( d,m,n, V, C ) end
           HPC(V) = begin C = Dict{Symbol,AbstractArray}(); m, n = size(V); 
              new( m,m,n, V, C ) end
           HPC(V,C) = begin m,n = size(V); new( m,m,n, V, C )  end
           HPC(d,V,C) = begin m,n = size(V); new( d,m,n, V, C )  end
           HPC(d,m,n, V,C) = new( d,m,n, V,C )
        end
ERROR: invalid redefinition of constant HPC
Stacktrace:
 [1] top-level scope
   @ REPL[4]:1

You are defining a new type HPC, and then you try to overwrite that definition by binding it to a variable called HPC.

Just write mutable struct HPC, don’t try to assign it to a variable.

1 Like

Thank you !! I’m very stupid.

Haha, don’t say that :smile: We all make stupid mistakes, without being stupid :wink:

5 Likes

Also note that a lot of your constructors are indistinguishable from one another. For example:

HPC(m,n) = ...
HPC(V,C) = ...

the latter re-defines the former, because the compiler does not have any indication which one to call for e.g. HPC(1, 2)
You’d have to add type constraints to distinguish them and create two different methods of the same function. For example:

HPC(m::Int, n::Int) = ...
HPC(V::Matrix{Float64}, C::Dict) = ...

The same is true for at least HPC(d,m,n) and HPC(d,V,C)

5 Likes

Hm, yes, there are several issues with the constructors. For example:

NaN is a float, not a valid Int, and Matrix{Float64} is a DataType, a type variable not an instance of a Matrix, same for the Dict.

2 Likes