# Struct variables explicit setting

**URL:** https://discourse.julialang.org/t/struct-variables-explicit-setting/57396
**Category:** New to Julia
**Created:** [March 17, 2021, 4:11pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396 "2021-03-17T16:11:29Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [March 17, 2021, 4:11pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/1 "2021-03-17T16:11:29Z")

</div>

Hi!  
I know how to do this:

```julia
struct a
       a1
       a2
       a3
end
b=a(1,2,3)

```

I was wondering if there’s a way to do this in a more explicit way. Something like: `b=a(a1=1,a2=2,a3=3)`.  
The reason would be readability (so one would know what the variables of the struct are without having to know the right order to put them in. So ideally, `b=a(a3=3,a1=1,a2=2)` would also work.  
Is this supported or would it take a lot of code to achieve (like making some fancy functions)?

Also, any disadvantage of using mutable structs vs regular ones?  
Thanks a lot!

---

<div class="post-metadata">

### Author: ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)
#### Post date: [March 17, 2021, 4:15pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/2 "2021-03-17T16:15:52Z")

</div>

[https://github.com/mauro3/Parameters.jl](https://github.com/mauro3/Parameters.jl) may be helpful, or just `Base.@kwdef`:

```julia
Base.@kwdef struct a
    a1
    a2
    a3
end

```

---

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [March 17, 2021, 4:21pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/3 "2021-03-17T16:21:20Z")

</div>

Cool, `@kwdef` does exactly what I had in mind. Thanks!

Are there any disadvantages of storing large arrays inside mutable structs?

---

<div class="post-metadata">

### Author: ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)
#### Post date: [March 17, 2021, 4:27pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/4 "2021-03-17T16:27:34Z")

</div>

> [@Ribeiro](#):
>
> Are there any disadvantages of storing large arrays inside mutable structs?

I’d generally go with a `struct` over a `mutable struct` where possible, but it really depends on the application. Good idea to provide your type definitions with concrete field types as a first step: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-fields-with-abstract-containers). Most of that page is worth a good read if you want to get decent performance 🙂

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [March 17, 2021, 4:27pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/5 "2021-03-17T16:27:53Z")

</div>

If you want a more customized solution than offered by Base.@kwdef, you need to write your own constructor methods: [Constructors · The Julia Language](https://docs.julialang.org/en/v1.7-dev/manual/constructors/)

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [March 17, 2021, 4:28pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/6 "2021-03-17T16:28:49Z")

</div>

> [@Ribeiro](#):
>
> Are there any disadvantages of storing large arrays inside mutable structs?

You better start another thread for this, different, question.

---

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [March 17, 2021, 5:06pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/7 "2021-03-17T17:06:00Z")

</div>

> [@mike](#):
>
> Good idea to provide your type definitions with concrete field types

I’m planning on using structs to avoid passing too many variables to functions and having messy code. All my variables will have very defined types. Would this be a good way to do it, performance-wise?

```julia-auto
mutable struct thing
a::Float64
b::Array{StaticArrays.SVector{3,Float64}}
c::Array{StaticArrays.SVector{Int}} # this one can have 3 or 4 elements in the SArray. Not sure how to declare that
d::Array{Float64}
end

```

Thanks!

> [@nsajko](#):
>
> You better start another thread for this, different, question.

Fair enough!

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [March 17, 2021, 5:13pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/8 "2021-03-17T17:13:46Z")

</div>

> [@Ribeiro](#):
>
> `Array{Float64}`

I think you’ll probably want to use `Vector{T}` instead of `Array{T}`, or explicitly specify the second type parameter (number of dimensions). E.g., `Array{T, 3}`.

---

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [March 17, 2021, 7:11pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/9 "2021-03-17T19:11:45Z")

</div>

`Array` also seems to work. What’s the difference between using `Array` and `Vector` here?

---

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [March 17, 2021, 7:20pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/10 "2021-03-17T19:20:57Z")

</div>

Oh, `Array` works for `Vector` and `Matrix`. So I should use those to be more precise, I guess.

How do I specify the array of 3 or 4 StaticArrays?  
Thanks!

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [March 17, 2021, 7:53pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/11 "2021-03-17T19:53:26Z")

</div>

> Array works for Vector and Matrix

Take a look at: [Multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1.7-dev/manual/arrays/) Quote from there: “An array is a collection of objects stored in a multi-dimensional grid.”

The second type parameter to Array determines the number of dimensions. So Array is more general than you think: it’s not just Vectors and matrices.

> [@Ribeiro](#):
>
> How do I specify the array of 3 or 4 StaticArrays?

I’m not quite sure what you’re asking here?

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [March 17, 2021, 7:58pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/12 "2021-03-17T19:58:23Z")

</div>

> [@Ribeiro](#):
>
> `Array` also seems to work. What’s the difference between using `Array` and `Vector` here?

`Array{T}`, as opposed to `Array{T, N}`, for some constant T and N, is this kind of type: [Types · The Julia Language](https://docs.julialang.org/en/v1.7-dev/manual/types/#UnionAll-Types) I’m not yet sure how do those work, but I suspect that using them as fields in a struct may lead to unintended effects.

---

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [March 17, 2021, 8:06pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/13 "2021-03-17T20:06:46Z")

</div>

I mean how do I declare that a variable in my struct will be like this one:

```julia
a=[SA[1,2,3],SA[1,2,3,4],SA[1,2,3];

```

I’d think something like `a::Vector{StaticArrays.SVector{Int}}`, but that does not work.  
Thanks!

---

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [March 17, 2021, 8:14pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/14 "2021-03-17T20:14:52Z")

</div>

This is ugly, but works:  
`Array{SArray{S,Int64,1,L} where L where S<:Tuple,1}`  
Thanks for the help, folks!

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [March 17, 2021, 8:15pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/15 "2021-03-17T20:15:51Z")

</div>

> [@Ribeiro](#):
>
> This is ugly, but works:  
> `Array{SArray{S,Int64,1,L} where L where S<:Tuple,1}`

That’s a UnionAll type (I mentioned those above). I _think_ using a (small) Union type is better than a UnionAll:

```julia
julia> T = Int
Int64

julia> Union{SVector{3, T}, SVector{4, T}}[SVector{3, T}([1,2,3]), SVector{4, T}([1,2,3,4]), SVector{3, T}([1,2,3])]
3-element Array{Union{SArray{Tuple{3},Int64,1,3}, SArray{Tuple{4},Int64,1,4}},1}:
 [1, 2, 3]
 [1, 2, 3, 4]
 [1, 2, 3]

```

On the other hand, maybe you just need Tuples? What are your requirements? If you want a better answer, you should probably say what is the problem you’re solving, and give more examples of the values that will be of the type you are seeking for.

---

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [March 17, 2021, 8:28pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/16 "2021-03-17T20:28:29Z")

</div>

Ah, so `a::Vector{Union{ SVector{3,Int},SVector{4,Int} }}`.  
I’m working on a problem that solves equations on large meshes. The vector above contains indices of the nodes of each element in my mesh (which can be a tri or a quad, hence the 3 or 4 condition). I usually use this vector to list all nodes in an element (`points[elements[n]]`, where `points` is a Vector of StaticArrays containing the x/y/z coordinates of each point, and `elements` is the Vector of StaticArrays I mentioned above.  
Does the information change how I should go around doing this? I’ve been using Julia for a month now, so I’m sure I’m doing many things wrong =P.  
Thanks!

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [March 17, 2021, 8:42pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/17 "2021-03-17T20:42:33Z")

</div>

I’m also new to Julia, but here’s an idea if you need speed:  
Instead of a single Vector, use two Vectors: a `Vector{SVector{3,Int}}` and a `Vector{SVector{4,Int}}`. This would mean more code for you to write; but assuming there’s a lot of data in the Vector(s), I think iterating over two Vectors of simpler types would be faster than iterating over a single vector of a more complex type.

---

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [March 17, 2021, 8:45pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/18 "2021-03-17T20:45:07Z")

</div>

Huh. That could work. I’ll look into it.  
Thanks!

---

<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: [March 17, 2021, 9:22pm UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/19 "2021-03-17T21:22:56Z")

</div>

> [@nsajko](#):
>
> Instead of a single Vector, use two Vectors: a `Vector{SVector{3,Int}}` and a `Vector{SVector{4,Int}}` . This would mean more code for you to write; but assuming there’s a lot of data in the Vector(s), I think iterating over two Vectors of simpler types would be faster than iterating over a single vector of a more complex type.

[GitHub - tkoolen/TypeSortedCollections.jl: Type-stable operations on type-heterogeneous collections · GitHub](https://github.com/tkoolen/TypeSortedCollections.jl) generalizes this idea to arbitrary collections of objects, as long as there are relatively few different types of things you want to store.

---

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [March 18, 2021, 11:09am UTC](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396/20 "2021-03-18T11:09:27Z")

</div>

> [@rdeits](#):
>
> [GitHub - tkoolen/TypeSortedCollections.jl: Type-stable operations on type-heterogeneous collections](https://github.com/tkoolen/TypeSortedCollections.jl) generalizes this idea to arbitrary collections of objects, as long as there are relatively few different types of things you want to store.

This is getting a bit advanced for me, but I’ll definitely have a look. Thanks!

[Next page](https://discourse.julialang.org/t/struct-variables-explicit-setting/57396.md?page=2)
