Macro for unpack and copy

Hi,
I’m frequently use UnPack.jl to unpack data.
For example,

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,

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?

1 Like

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.

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

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

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

For example:

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

Not sure about it though.

1 Like