# Understanding of mutable struct

**URL:** <https://discourse.julialang.org/t/understanding-of-mutable-struct/19206>\
**Category:** General Usage\
**Created:** [January 2, 2019, 8:34pm UTC](https://discourse.julialang.org/t/understanding-of-mutable-struct/19206 "2019-01-02T20:34:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carsten](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Carsten](https://discourse.julialang.org/u/Carsten)\
**Post date:** [January 2, 2019, 8:34pm UTC](https://discourse.julialang.org/t/understanding-of-mutable-struct/19206/1 "2019-01-02T20:34:05Z")

</div>

Suppose I define a **mutable struc** as follows:

```julia
mutable struct Np <: Number
   a::Float64
   b::Int
   c::ComplexF64
   d::Float64
end

```

Assigning numerical values:

```julia
np = Np(1.2,
3,
1+4im,
3
);

```

which results in:

```julia
> julia> np
Np(1.2, 3, 1.0 + 4.0im, 3.0)

```

This is what I expected. Doing the same with strings:

```julia
mutable struct Sp <: AbstractString
   a::String
   b::String
   c::String
   d::String
end

```

Again, assinging values:

```julia
> julia> sp = Sp("a1",
       "b1",
       "c1",
       "d1"
       );

```

with no error message. Bur when I try to show the single elements (as I did above for np) I’ve got the error message:

```julia
"Error showing value of type Sp:
ERROR: MethodError: no method matching iterate(::Sp)
Closest candidates are:
  iterate(::AbstractString, ::Integer) at strings/basic.jl:135
  iterate(::Core.SimpleVector) at essentials.jl:589
  iterate(::Core.SimpleVector, ::Any) at essentials.jl:589

```

However, the single elements are accessible:

```julia
julia> sp.a
"a1"

```

Is there a specific reason why string types in structures behave different compared to number types?

Carsten

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 2, 2019, 8:38pm UTC](https://discourse.julialang.org/t/understanding-of-mutable-struct/19206/2 "2019-01-02T20:38:06Z")

</div>

When you do `Sp <: AbstractString` you will opt in to using the `AbstractString` methods already defined. `show` on an `AbstractString` tries to iterate it and show the characters in it. This fails for your type since it doesn’t provide iteration.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 2, 2019, 8:46pm UTC](https://discourse.julialang.org/t/understanding-of-mutable-struct/19206/3 "2019-01-02T20:46:50Z")

</div>

I’ve edited your post to quote the code, making it more readable. Please see

> [@PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530):
>
> This is a short post on how to use backticks (` and ```) to quote code, so it is easy to read. This post can be linked to new users who are confused by this feature. Why By quoting your code, you get a monospaced font (which preserves indentation) and syntax highlighting. This makes it easier to read your code and help you. Displayed code looks like this: function displayed\_code(x::Int, y::Int) if x \< y println("x is smaller") else println("or not") end end Inline…

for details on how to quote your code yourself.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 2, 2019, 8:51pm UTC](https://discourse.julialang.org/t/understanding-of-mutable-struct/19206/4 "2019-01-02T20:51:46Z")

</div>

Also note that you probably don’t want `Np <: Number` either—it means that `Np` is a kind of number just like `Sp <: AbstractString` means that `Sp` is a kind of string. It has nothing to do with the types of the fields of these structures, which can be anything.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 2, 2019, 11:04pm UTC](https://discourse.julialang.org/t/understanding-of-mutable-struct/19206/5 "2019-01-02T23:04:50Z")

</div>

I think you want:

```julia
julia> mutable struct Sp{T <: AbstractString}
          a::T
          b::T
          c::T
          d::T
       end

julia> sp = Sp("a1",
              "b1",
              "c1",
              "d1"
              )
Sp{String}("a1", "b1", "c1", "d1")

```
