# Macro for unpack and copy

**URL:** https://discourse.julialang.org/t/macro-for-unpack-and-copy/63416
**Category:** New to Julia
**Tags:** macros, struct, copy
**Created:** [June 23, 2021, 5:36am UTC](https://discourse.julialang.org/t/macro-for-unpack-and-copy/63416 "2021-06-23T05:36:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 23, 2021, 5:36am UTC](https://discourse.julialang.org/t/macro-for-unpack-and-copy/63416/1 "2021-06-23T05:36:27Z")

</div>

Hi,  
I’m frequently use [UnPack.jl](https://github.com/mauro3/UnPack.jl) to unpack data.  
For example,

```julia
struct MyStr
    a
    b
end
mystr = MyStr(1, 2)
@unpack a, b = mystr # a, b = mystr.a, mystr.b

```

However, I sometimes need copy the unpacked values. For example,

```julia
struct MyStr
    a
    b
end
mystr = MyStr(1, 2)
@unpack a, b = mystr # a, b = mystr.a, mystr.b
a = copy(a)
b = copy(b)

```

Is there any macro that “unpack and copy” variables?

---

<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: [June 23, 2021, 6:36am UTC](https://discourse.julialang.org/t/macro-for-unpack-and-copy/63416/2 "2021-06-23T06:36:40Z")

</div>

If it is specific to a type, you could define a special unpack method. See the last example in the README.

Otherwise, a feature could be to be able to specify a function to be applied before unpacking. In a sense similar to say `sum` which allows a function as input which is then applied to each element before summing. Not sure about the syntax though.

---

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 23, 2021, 6:52am UTC](https://discourse.julialang.org/t/macro-for-unpack-and-copy/63416/3 "2021-06-23T06:52:37Z")

</div>

> [@mauro3](#):
>
> Otherwise, a feature could be to be able to specify a function to be applied before unpacking. In a sense similar to say `sum` which allows a function as input which is then applied to each element before summing. Not sure about the syntax though.

For me, it’s an interesting way.  
How about this form?

```julia
@unpack(:copy, a = mystr) # a = copy(mystr.a)

```

---

<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: [June 23, 2021, 7:16am UTC](https://discourse.julialang.org/t/macro-for-unpack-and-copy/63416/4 "2021-06-23T07:16:51Z")

</div>

That would need parenthesis for several outputs (unless you insist on the “:”, which I find odd)

For example:

```julia
@unpack copy a, b = A
@unpack(copy, (a, b = A))

```

Not sure about it though.
