# Initialize a variable inside a struct

**URL:** <https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 30, 2020, 11:42am UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967 "2020-07-30T11:42:18Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johncowk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johncowk/32/8032_2.png) [@Johncowk](https://discourse.julialang.org/u/Johncowk)\
**Post date:** [July 30, 2020, 11:42am UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967/1 "2020-07-30T11:42:18Z")

</div>

I am not a very good Julia programmer (despite reading the doc numerous times).  
For what I am trying to code at the moment, I am trying to create something close to a Python class.

I thought of using a mutable struct, but I realized that one cannot initialize a variable as one of it’s argument. Something like

```nohighlight
mutable struct Test
a
b
c = 3
end

```

returns the error

```nohighlight
ERROR: syntax: "c=3" inside type definition is reserved
Stacktrace:
 [1] top-level scope at none:0

```

Ultimatively what I want to do would be something like a

```julia
Class Test
    def __init__ (self, var1, var2):
         self.var1 = var1
         self.var2 = var2
         self.matrix = foo(shape(var1))

```

And I don’t know how to do this in Julia if not for declaring `matrix` as a `const` outside of the `struct` which would be unpracticle.  
I have already read the doc on constructors and composite types so please do not just tell me to “read the doc again”.

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [July 30, 2020, 11:51am UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967/2 "2020-07-30T11:51:02Z")

</div>

```julia
mutable struct Test
a::Float64
b::String
c::Int
end

test_object = Test(1.0, "Test", 1)

```

You don’t have Classes in Julia. Instead you first define the data structure (`struct`, e.g. what kind of data your objects should hold) and what type of data the structure will hold. Afterwards you create instances of those “objects” with concrete values for the variables (could also be variables from somewhere else).

You can then access this data directly if you want to:

```julia
julia> test_object.a
1.0

julia> test_object.b
"Test"

julia> test_object.c
1

```

Does this help you?

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [July 30, 2020, 12:10pm UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967/3 "2020-07-30T12:10:16Z")

</div>

Ah I see you’ve changed your question a bit since I first read it.

Now I would personally just do the following things to do what you want:

- create a struct and add a field in the struct that is able to hold the desired matrix, i.e. has the appropriate type (e.g. `c::Array{Int64, 2}` or whatever you need)

```julia
mutable struct Test
a::Int
b::Int
c::Array{Int, 2}

```

- create an anonymous function that creates the desired matrix (e.g. `foo(a, b) = [a b]`)
- initialise the `object = Test(1, 2, [0 0])` with an empty Array for `c`
- update the array to reflect what you want it to be (e.g. `object.c = foo(object.a, object.b)`)

However, there might be better ways to do it.

Edit: Instead of writing `[0 0]` you could also write `zeros(Int, 1, 2)`. This would be the preferred way to do it once your matrices become a lot bigger.

---

<div class="post-metadata">

**Author:** ![Johncowk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johncowk/32/8032_2.png) [@Johncowk](https://discourse.julialang.org/u/Johncowk)\
**Post date:** [July 30, 2020, 12:18pm UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967/4 "2020-07-30T12:18:35Z")

</div>

That is actually how I wanted to do it, but it feels a bit sloppy and forces me to include an extra input variable unpon instanciation so I thought maybe there is a better way to do it. Thanks for your help.

Yes my fnger slipped and pressed enter before I could properlly finish writting my question that’s why !

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [July 30, 2020, 12:22pm UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967/5 "2020-07-30T12:22:05Z")

</div>

No worries, it’s not as if you could have anticipated my eagerness to help. 😃 Normally it’s no problem to rewrite parts of your question before people find the time to answer.

If there’s a better way to do what you want to do, there will surely be somebody coming along to tell us how.

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [July 30, 2020, 2:14pm UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967/6 "2020-07-30T14:14:28Z")

</div>

You can define an outer constructor. Something like:

```julia
struct Foo
    var1
    var2
    matrix
end

Foo(a,b) = Foo(a,b,zeros(a,b))

```

or an inner constructor:

```julia
struct Foo
    var1
    var2
    matrix
    Foo(a,b,m=zeros(a,b)) = new(a,b,m)
end

```

or, if you want to enforce a specific value for the matrix (not allow the caller to override it),

```julia
struct Foo
    var1
    var2
    matrix
    Foo(a,b) = new(a,b,zeros(a,b))
end

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 30, 2020, 2:23pm UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967/7 "2020-07-30T14:23:01Z")

</div>

The `Base.@kwdef` macro allows you to use the `c = 3` syntax:

```julia
julia> Base.@kwdef mutable struct Test
         a
         b
         c = 3
       end
Test

julia> t = Test(a=1, b=2)
Test(1, 2, 3)

```

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [July 30, 2020, 2:38pm UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967/8 "2020-07-30T14:38:13Z")

</div>

> [@rdeits](#):
>
> The `Base.@kwdef` macro allows you to use the `c = 3` syntax

I think this is actually the solution to OP’s question. Way more in line with what they were hoping to be able to write.

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [July 30, 2020, 3:51pm UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967/9 "2020-07-30T15:51:18Z")

</div>

Wow! This answered I question that (1) I didn’t know I had and (2) if I’d known I had it I did not have the vocabulary to ask it.

Thanks.

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [July 31, 2020, 4:23pm UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967/10 "2020-07-31T16:23:22Z")

</div>

Does Base.@kwdef work in Julia 1.0? I stuck that in something I’m working on and CI failed on 1.0. It was happy on 1.4, 1.5, and the nightly.

The problem might be elsewhere, like a few fresh BLAS calls.

---

<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:** [July 31, 2020, 6:23pm UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967/11 "2020-07-31T18:23:44Z")

</div>

I tested in a copy I had lying around (1.0.5), and it works. Just note that the constructor becomes: `t = Test(a=1, b=2)` not `t = Test(1, 2)` (i.e., if you do not want to pass the fields with default value, you need to pass the other fields as named arguments, the positional constructor keeps existing and expecting three arguments).

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 31, 2020, 6:46pm UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967/12 "2020-07-31T18:46:18Z")

</div>

For a more featureful version of `@kwdef`, you can also check out [https://github.com/mauro3/Parameters.jl](https://github.com/mauro3/Parameters.jl)

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [July 31, 2020, 9:32pm UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967/13 "2020-07-31T21:32:26Z")

</div>

Thanks @Henrique_Becker and @rdeits.

It seems my issues are a bit deeper than that. I’m using the mp! from 1.3 now and, having heard “The State of Julia” will be moving to 1.5-dependent things as soon as it’s released.

So, my 1.0 days are behind me. I need the non-allocating array slices bad. Using those will clean up my code in a significant way. I’m also happy to hear about the Givens rotations.

---

<div class="post-metadata">

**Author:** ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)\
**Post date:** [August 1, 2020, 1:41am UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967/14 "2020-08-01T01:41:39Z")

</div>

Does this hep?

```julia
function shape(x)
    return 3*x
end

struct Test
   a :: Int64
   b :: Int64
   c :: Int64
   function Test(x, y)
      return new(x, y, shape(x))
   end
end

julia> Test(1, 2)
Test(1, 2, 3)
```

---

<div class="post-metadata">

**Author:** ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)\
**Post date:** [August 1, 2020, 1:46am UTC](https://discourse.julialang.org/t/initialize-a-variable-inside-a-struct/43967/15 "2020-08-01T01:46:40Z")

</div>

Or even simpler:

```julia
shape(x) = 3*x

struct Test
   a :: Int64
   b :: Int64
   c :: Int64
end

Test(x, y) = Test(x, y, shape(x))

julia> Test(1, 2)
Test(1, 2, 3)

```
