# Strange behavior of \`struct\` definition in REPL

**URL:** <https://discourse.julialang.org/t/strange-behavior-of-struct-definition-in-repl/103116>\
**Category:** General Usage\
**Created:** [August 23, 2023, 4:04pm UTC](https://discourse.julialang.org/t/strange-behavior-of-struct-definition-in-repl/103116 "2023-08-23T16:04:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pao/32/34395_2.png) [@pao](https://discourse.julialang.org/u/pao)\
**Post date:** [August 23, 2023, 4:04pm UTC](https://discourse.julialang.org/t/strange-behavior-of-struct-definition-in-repl/103116/1 "2023-08-23T16:04:00Z")

</div>

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?

```julia
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

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 23, 2023, 4:09pm UTC](https://discourse.julialang.org/t/strange-behavior-of-struct-definition-in-repl/103116/2 "2023-08-23T16:09:02Z")

</div>

> [@pao](#):
>
> `HPC = mutable struct HPC`

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.

---

<div class="post-metadata">

**Author:** ![pao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pao/32/34395_2.png) [@pao](https://discourse.julialang.org/u/pao)\
**Post date:** [August 23, 2023, 4:12pm UTC](https://discourse.julialang.org/t/strange-behavior-of-struct-definition-in-repl/103116/3 "2023-08-23T16:12:08Z")

</div>

Thank you !! I’m very stupid.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 23, 2023, 4:14pm UTC](https://discourse.julialang.org/t/strange-behavior-of-struct-definition-in-repl/103116/4 "2023-08-23T16:14:35Z")

</div>

Haha, don’t say that 😄 We all make stupid mistakes, without being stupid 😉

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [August 23, 2023, 7:52pm UTC](https://discourse.julialang.org/t/strange-behavior-of-struct-definition-in-repl/103116/5 "2023-08-23T19:52:11Z")

</div>

> [@pao](#):
>
> ```julia
> # inner constructors
> 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 )
> 
> ```

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

```julia
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:

```julia
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)`

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 23, 2023, 10:53pm UTC](https://discourse.julialang.org/t/strange-behavior-of-struct-definition-in-repl/103116/6 "2023-08-23T22:53:24Z")

</div>

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

> [@pao](#):
>
> ```julia
> HPC() = new( NaN, NaN, NaN, Matrix{Float64}, Dict{Symbol, AbstractArray} )
> 
> ```

`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`.
