# Reuse declarations

**URL:** <https://discourse.julialang.org/t/reuse-declarations/27001>\
**Category:** General Usage\
**Created:** [July 30, 2019, 9:57pm UTC](https://discourse.julialang.org/t/reuse-declarations/27001 "2019-07-30T21:57:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 30, 2019, 9:57pm UTC](https://discourse.julialang.org/t/reuse-declarations/27001/1 "2019-07-30T21:57:50Z")

</div>

I want to declare two structures, one of which contains many of the same elements of the other. In a simple example, it would be, for example:

```julia
struct A
   a :: Int64
end
struct B
  a :: Int64
  b :: Float64
end

```

What I am aiming is to be able to write something like (the following example does not work, but probably makes the point):

```julia
DataA = "a :: Int64"
struct A
  eval(DataA)
end
struct B
  eval(DataA)
  b :: Int64
end

```

Of course, this would be used in a situation where the DataA is a long list of properties.

The same sort of construct could be used to define two structures with the same data structure, one mutable and the other not, for instance:

```julia
DataA = "a :: Int64"
struct A
  eval(DataA)
end
mutable struct MutA
  eval(DataA)
end

```

Actually I would like to do that to organize some data reading code, by reading the data first to a mutable structure and then passing it to an unmutable one.

I searched the docs for metaprograming options, but I could not find out how to make that work.

Thanks for the thoughts.  
Leandro.

---

<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:** [July 30, 2019, 10:06pm UTC](https://discourse.julialang.org/t/reuse-declarations/27001/2 "2019-07-30T22:06:25Z")

</div>

```julia
DataA = :(a :: Int64)

macro paste(ex)
    ex = eval(ex)
    :($(esc(ex)))
end

struct A
  @paste DataA
end
struct B
  @paste DataA
  b :: Int64
end

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 30, 2019, 10:38pm UTC](https://discourse.julialang.org/t/reuse-declarations/27001/3 "2019-07-30T22:38:05Z")

</div>

> [@kristoffer.carlsson](#):
>
> `macro paste(ex)`

Probably easier to just do:

```julia
DataA = :(a::Int64)
@eval struct A
    $DataA
end

```

rather than defining a `@paste` macro.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 30, 2019, 11:32pm UTC](https://discourse.julialang.org/t/reuse-declarations/27001/4 "2019-07-30T23:32:31Z")

</div>

Thank, both options are great.

What about initializing the immutable structure with the data from the mutable one?  
For instance, something like:

```julia
DataA = quote
  x 
  y
end
@eval struct A
  $DataA
end
@eval struct MutA
   $DataA
end
MutA() = MutA(0,0) # wandering if there is a better way to initialize these values as well
z = MutA()
z.a = 1
z.b = 2
w = A(expand(z)) # something like this

```

Where the `expand` function returns the list of values of `z`. For instance, this function does that, but it returns a string and thus the definition of `w` fails:

```julia
function expand(a)
  nfields = length(fieldnames(typeof(a)))
  ifield = 0
  list = ""
  for field in fieldnames(typeof(a))
    ifield = ifield + 1
    if ifield < nfields
      list = "$list$(@eval a.$field),"
    else
      list = "$list$(@eval a.$field)"
    end
  end
  return list
end

```

I understand that I could build explicitly a constructor, such as

```julia
A( x :: MutA ) = A( x.a, x.b )

```

but again, for structures with lots of fields that might become cumbersome.

Any idea? Thanks.

---

<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:** [July 30, 2019, 11:39pm UTC](https://discourse.julialang.org/t/reuse-declarations/27001/5 "2019-07-30T23:39:14Z")

</div>

As a general note, avoid using strings for metaprogramming. It really isn’t the right tool in a language that has first class support for constructing expressions.

If you just want to shove the fields from one type into another (and performance is not crucial) in the same order you can do something like

```julia
julia> A(getfield.((z,), 1:nfields(z))...)
A(1, 2)

```

But having two copies of a struct, with one being mutable seems pretty odd.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 30, 2019, 11:51pm UTC](https://discourse.julialang.org/t/reuse-declarations/27001/6 "2019-07-30T23:51:21Z")

</div>

Great, thanks for the help.

The idea of having a mutable structure with the same data structure as the immutable one is that I need to read a lot of different parameters from a file, and then create an immutable structure with all that data. Thus, since the immutable structure has to be generated at once,

```julia
x = A(1,2,"abc", ... )

```

I need to create temporary variables for all the fields anyway, read all these temporary variables and, at the end, create the immutable `x`.

What I was thinking was to use a mutable structure with the same structure of the immutable one to read clearly and beautifully all the data, using something as:

```julia
y = MutA()
y.a = data[1]
y.b = parse(Float64,somestring)

```

to finally initalize the immutable `x` variable, which will be used in performance critical code, using, as you suggest,

```julia
x = A(getfield.((y,),1:nfields(y))...)

```

Perhaps I am missing something trivial about immutable constructors which would turn out the task much easier?

---

<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:** [July 30, 2019, 11:55pm UTC](https://discourse.julialang.org/t/reuse-declarations/27001/7 "2019-07-30T23:55:51Z")

</div>

> [@lmiq](#):
>
> Thus, since the immutable structure has to be generated at once,

I don’t see why this is a problem?

For example, if you read it as a Dict you could use Parameters.jl as

```julia
julia> d = Dict(:mass => 2, :velocity => 10, :name => "foo")
Dict{Symbol,Any} with 3 entries:
  :name => "foo"
  :mass => 2
  :velocity => 10

julia> using Parameters

julia> @with_kw struct MyParameters
           name
           mass
           velocity
       end
MyParameters

julia> MyParameters(;d...)
MyParameters
  name: String "foo"
  mass: Int64 2
  velocity: Int64 10

```

Or if you just want to splat a vector

```julia
julia> v = ["foo", 2, 10];

julia> MyParameters(v...)
MyParameters
  name: String "foo"
  mass: Int64 2
  velocity: Int64 10

```

I don’t ever see a need to use two different structs with the same fields.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 30, 2019, 11:59pm UTC](https://discourse.julialang.org/t/reuse-declarations/27001/8 "2019-07-30T23:59:22Z")

</div>

To be truth I didn’t know the existence of the “…” syntax to expand the elements of a list in to a function call.

That is great, thank you.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 31, 2019, 12:11am UTC](https://discourse.julialang.org/t/reuse-declarations/27001/9 "2019-07-31T00:11:36Z")

</div>

Just to finish: I actually like the result obtained using the two structures, one mutable and the other not. Of course, it is only about how easy is to understand the code afterwards. But one example of the kind of thing I am aiming to use is:

```julia
AtomData = quote
  index :: Int64
  residue :: String
  name :: String
  x :: Float64
  y :: Float64
  z :: Float64
  b :: Float64
  occup :: Float64
end

@eval mutable struct ReadAtom
  $AtomData
end
ReadAtom() = ReadAtom(0,"X","X",0.,0.,0.,0.,0.)

@eval struct Atom
  $AtomData
end
Atom( atom :: ReadAtom ) = Atom([getfield(atom,i) for i in 1:nfields(atom)]...)

```

The only think I would like to improve here is the constructor for the mutable structure without parameters, which I still have to modify if I add an additional parameter to the AtomData list. Ideally I would like to provide default parameters already there.

---

<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:** [July 31, 2019, 12:18am UTC](https://discourse.julialang.org/t/reuse-declarations/27001/10 "2019-07-31T00:18:45Z")

</div>

I don’t see the use of the mutable struct at all here. You could just have directly defined

```julia
julia> Atom() = Atom(0,"X","X",0.,0.,0.,0.,0.)
Atom

julia> Atom()
Atom(0, "X", "X", 0.0, 0.0, 0.0, 0.0, 0.0)

```

and used that?

To set default values, Parameters.jl is good again:

```julia
julia> using Parameters

julia> @with_kw struct Atom
           index::Int64 = 0
           residue :: String = "X"
           name :: String = "X"
           x :: Float64 = 0
           y :: Float64 = 0
           z :: Float64 = 0
           b :: Float64 = 0
           occup :: Float64 = 0
       end
Atom

julia> Atom()
Atom
  index: Int64 0
  residue: String "X"
  name: String "X"
  x: Float64 0.0
  y: Float64 0.0
  z: Float64 0.0
  b: Float64 0.0
  occup: Float64 0.0

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 31, 2019, 12:33am UTC](https://discourse.julialang.org/t/reuse-declarations/27001/11 "2019-07-31T00:33:29Z")

</div>

The use of `Parameters` to initialize the values is what I want indeed.

However, using simply `Atom()` would not allow me to actually fill this variable  
with the actual data (edit: using the names of the fields), which will be read afterwards from input files, since the Atom  
structure is immutable.

But I completely understand that I could replace the mutable structure with a simple  
list, or with a Dict. The choice relies only on how the code looks like. I like the idea of using

```julia
x = ReadAtom() # x is muttable and will be used for data reading
x.index = 1
x.name = "Carbon"

y = Atom(x) # y is immutable and will be used in critical code

```

Thank you very much for all your help. I learnt a lot of things.

EDIT:

An alternative way to initialize with default parameters a muttable struct, which is convenient in my case, is this:

```julia
function initdata(X :: DataType)
  list = []
  for type in X.types
    if type == Int64
      push!(list,0)
    end
    if type == String
      push!(list,"X")
    end
    if type == Float64
      push!(list,0.)
    end
  end
  y = X(list...)
  return y
end

```

with which the actual code would look like:

```julia
x = initdata(ReadAtom) # muttable with default parameters
x.index = 1
x.name = "Carbon"

y = Atom(x) # immutable

```
