# Const fields in structs

**URL:** <https://discourse.julialang.org/t/const-fields-in-structs/5994>\
**Category:** General Usage\
**Created:** [September 20, 2017, 9:03am UTC](https://discourse.julialang.org/t/const-fields-in-structs/5994 "2017-09-20T09:03:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)\
**Post date:** [September 20, 2017, 9:03am UTC](https://discourse.julialang.org/t/const-fields-in-structs/5994/1 "2017-09-20T09:03:10Z")

</div>

Version 0.6.0 (2017-06-19 13:05 UTC) x86\_64-redhat-linux

Am I right, that const fields are not supported? It is possible to define a `struct` with `const` fields, but how to create an instance of it?

```julia
mutable struct F1
     a::Int
     const c = [1, 1]
end
# ok

f1 = F1(3) # fails
f1 = F1(3, [7, 8]) # fails also

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [September 20, 2017, 9:06am UTC](https://discourse.julialang.org/t/const-fields-in-structs/5994/2 "2017-09-20T09:06:53Z")

</div>

What are you trying to do exactly? Have a field which is identical among all instances of a type, or prevent modification of the field after the object has been constructed? If the latter, that’s not possible AFAIK (Julia considers that fields are private and that it’s your problem if you modify them when you are not supposed to). If the former, you can use a `const` global variable (local to the module in which it is defined).

---

<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:** [September 20, 2017, 9:10am UTC](https://discourse.julialang.org/t/const-fields-in-structs/5994/3 "2017-09-20T09:10:57Z")

</div>

Ref [https://github.com/JuliaLang/julia/issues/9448](https://github.com/JuliaLang/julia/issues/9448), [https://github.com/JuliaLang/julia/pull/11430](https://github.com/JuliaLang/julia/pull/11430)

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 20, 2017, 9:39am UTC](https://discourse.julialang.org/t/const-fields-in-structs/5994/4 "2017-09-20T09:39:21Z")

</div>

To not answer the OP question: This is a bit an odd corner case of Julia which people stumble across sometimes. This sheds a bit of light on it:

```julia
julia> mutable struct F1
            a::Int
            const c = [1, 1]
           F1(a) = (@show c; new(a))
       end                                                                                                                                            
                                                                                                                                                      
julia> F1(4)                                                                                                                                          
c = [1, 1]                                                                                                                                            
F1(4)
                                                                                                                                                
julia> mutable struct F2
            a::Vector
            const c = [1, 1]
           F2(a) = (@show c; new(a))
           F2() = new(c)
       end                                                                                                                                            
                                                                                                                                                      
julia> F2()                                                                                                                                           
F2([1, 1])                                                                                                                                            

julia> ans.a[1] = 7                                                                                                                                   
7                                                                                                                                                     

julia> F2([1])                                                                                                                                        
c = [7, 1] # so c was mutated                                                                                                                                      
F2([1])

```

So, `c` is a variable inside the scope of the type definition. The only access to it, as far as I know is through inner constructors. There are some musings on what this could be useful here: [https://github.com/JuliaLang/julia/issues/9443](https://github.com/JuliaLang/julia/issues/9443).

However, what most people think this should do (at least without the `const`) is to assign a default value to the field, see [https://github.com/JuliaLang/julia/issues/10146](https://github.com/JuliaLang/julia/issues/10146). And this is also planned to be implemented. As part of this the following is now reserved:

```julia
julia> mutable struct F3
            a::Vector
            c = [1, 1]
       end
ERROR: syntax: "c=[1,1]" inside type definition is reserved

```

see [https://github.com/JuliaLang/julia/commit/f7a4aa3e7fcfd4c419809e34dc5d6ee485846298](https://github.com/JuliaLang/julia/commit/f7a4aa3e7fcfd4c419809e34dc5d6ee485846298). Apparently putting the `const` in front circumnavigates this reservation in the parser.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 20, 2017, 9:44am UTC](https://discourse.julialang.org/t/const-fields-in-structs/5994/5 "2017-09-20T09:44:09Z")

</div>

There is also [https://github.com/JuliaLang/julia/issues/18657](https://github.com/JuliaLang/julia/issues/18657), which states that the inner constructor should not be removed in this case.
