Blind destructuring of a tuple/NamedTuple?

Hello!

Could someone show me a way to achieve this:

v = (x=1, y=2)
@destructure v

println("x = $x, y = $y") 

Basically I want through a macro or some other way, to be able to take all the pairs/values in a tuple and assign them as variable = value, to be used later on in a function. I know of:

(x,y) = v

But I do not know if I will have 2 or N args. This is just a toy example.

Kind regards

NamedTuple destructuring is quite succinct and still explicit, and the number of values in v doesn’t matter:

(; x, y) = v

Just putting all keys into local scope sounds a bit risky to me.

1 Like

Agreed, sounds risky on paper, I just need to be able to do it to test something and see how I like it :slight_smile:

Managed risks are okay!

I agree, println("x = $(v.x), y = $(v.y)") would be a safer option.

You did notice the semicolon in my example though? ;

Its not in your MWE and is a very different thing (propertyname desctructuring as opposed to iterable destructuring)

1 Like

See Unpack named tuples (does anyone know how to do it?) for a previous discussion with a solution that comes with issues.

If you don’t know how args there are, how would you refer to them later?