# Static structs with constant field

**URL:** https://discourse.julialang.org/t/static-structs-with-constant-field/48009
**Category:** New to Julia
**Tags:** question
**Created:** [October 8, 2020, 11:04pm UTC](https://discourse.julialang.org/t/static-structs-with-constant-field/48009 "2020-10-08T23:04:00Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [October 8, 2020, 11:04pm UTC](https://discourse.julialang.org/t/static-structs-with-constant-field/48009/1 "2020-10-08T23:04:00Z")

</div>

Hey guys,

It’s there a way to declare a constant struct.

```julia
const struct A
    a = 1
    b = "value"
end

println(A.b)

```

Thy for your time

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [October 8, 2020, 11:25pm UTC](https://discourse.julialang.org/t/static-structs-with-constant-field/48009/2 "2020-10-08T23:25:47Z")

</div>

I’m not exactly sure I understand what you’re asking, but it looks like you’re mixing the definition of a `struct` with the construction of an instance of that struct.

I think what you want to do is this:

```julia
struct MyStruct
    a::Int
    b::String
end

const A = MyStruct(1, "value")

```

at the repl we see

```julia
julia> println(A.b)
value

```

Another thing you could do is use a `NamedTuple`, they’re a big like ‘anonymous structs’, e.g.

```julia
julia> const A = (;a=1, b="value")
(a = 1, b = "value")

julia> println(A.b)
value

```

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [October 8, 2020, 11:32pm UTC](https://discourse.julialang.org/t/static-structs-with-constant-field/48009/3 "2020-10-08T23:32:47Z")

</div>

I think what OP is looking for is more along the lines of `static` variables in OOP classes (shared value among all instances of the class). Julia doesn’t have these, but the typical way to get the same effect in Julia is to just use a global variable.

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [October 8, 2020, 11:57pm UTC](https://discourse.julialang.org/t/static-structs-with-constant-field/48009/4 "2020-10-08T23:57:05Z")

</div>

I like the NamedTuple. Didn’t think of that, but it’s basically what I need

---

<div class="post-metadata">

### Author: ![Koenraad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/koenraad/32/210376_2.png) [@Koenraad](https://discourse.julialang.org/u/Koenraad)
#### Post date: [August 16, 2024, 10:58am UTC](https://discourse.julialang.org/t/static-structs-with-constant-field/48009/5 "2024-08-16T10:58:24Z")

</div>

Hi you could add intern constructor:  
const struct A  
a  
b  
A=new(1,“value”)  
end

This causes the fields to be constant. They can’t be changed. It seems to me a way to group constant values. Is this correct?

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [August 16, 2024, 1:26pm UTC](https://discourse.julialang.org/t/static-structs-with-constant-field/48009/6 "2024-08-16T13:26:07Z")

</div>

1. Why are you answering a topic of _four_ ago?
2. Why `const struct`? `struct` without `mutable` in front of it is already immutable by default.
3. `A=new(1,“value”)`, I think you meant `A()=new(1,“value”)`

---

<div class="post-metadata">

### Author: ![Koenraad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/koenraad/32/210376_2.png) [@Koenraad](https://discourse.julialang.org/u/Koenraad)
#### Post date: [August 19, 2024, 8:35am UTC](https://discourse.julialang.org/t/static-structs-with-constant-field/48009/7 "2024-08-19T08:35:30Z")

</div>

1. Just starting Julia. Was looking for how to make code more clear without documentation.
2. Right but when you define constructor without parameters, even when defining a variable of the A type. values for a and b component will always be 1 and “value”. Since constructor is interally defined. i.e bad=A(2,“bad”) will give error. I saw there was a macro @kwdef. When you define here all fields you get same effect.```  
@kwdef struct A  
a=1  
b=“value”  
end

```julia
In fact you get a type which contains a singleton.
3. You are right about A() error. It should be A()=new(1,"value")
```
