Destructuring NamedTuples with Regular Expressions

As of Julia 1.7, one can deconstruct NamedTuples. I wanted to know if it’s possible to use regular expressions to do this, rather than specifying the variables to be deconstructured.


x = (; a11=1, a12=2, b1=3)

#the next is the feature I was looking for
(; r"^a") = x

right now, the only way I found is

(; a11, a12) = x

but I have a lot of variables and wanted to keep the code clean when I unpack the parameters.


one thing to consider is whether you want to use a namedtuple of arrays here by which I mean (a=[1,2], b=3).

My guess/opinion is that something like would be considered magical for base julia.

That said, you absolutely could write a macro to implement that functionality

macro regex_destruct(ex)
     # match regex against field names from right hand side
     # generate assignment statements for new names
     # wrap in esc(...) so new variables don't get gensym names

@regex_destruct  (; r"^a") = x

It is not clear to me the advantage of having the possibility of using regex to select all the variables of interest all at once, when then in the code they must still be “used” one by one by hand.
In any case, just to experience the possibilities of metaprogramming …

julia> x = (; a11=1, a12=2, b1=3, ba1=4)
(a11 = 1, a12 = 2, b1 = 3, ba1 = 4)
# ff=filter(f->startswith(string(f),"a"), propertynames(x))
julia> gg=filter(f->!isnothing(match(r"^a", string(f))), propertynames(x))
(:a11, :a12)

julia> eval.(Expr.(:(=) , gg,getproperty.(Ref(x),gg)))
(1, 2)

julia> a11

julia> a12

in @chris-b1 answer I would like to understand the use and necessity of escape(…)
(this is my second time I try to do some kind of metaprogramming)

Thanks for your answers! It seems that there only “hacks” to do this, then? I saw that Parameters.jl allows you to unpack all variables in one line, which is ultimately what I’m interested in. But this is only valid for struct, not for NamedTuples.
I just wanted to avoid adding the following line for each function I’m using

(; a11, a12) = x

fwiw, I find @unpack a11, a12 = x than (; a11, a12) = x

But ultimately, please write out the variables explicitly. Code which creates global variables without actually writing out the literal is very hard to read and maintain. Better for the future to get used to writing things out.

Yeah, I thought about whether it’s more convenient keeping the variables explicit. But sometimes you have around 20 parameters that use in every single function. I use Greek letters for those, so there’s no risk of confusion. It’s just becomes annoying to have one line unpacking the same variables in every function.

It’s the first time I’m working on a big project, so still learning how to deal this.I should probably inspect the possibility of using const now that it ensures type-stability in the new versions of Julia. Maybe I’ll create a PR to see if this feature is worth being added.

Btw, I use (; a11, a12) = x rather than @unpack a11, a12 = x because I think that Parameters will be eventually deprecated? See here. Even if it’s not deprecated, I want to be on the safe side and use Base functionality.