Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is my first PR, so not sure if it should be added. here. It's for a feature that could come in handy.
Given a NamedTuple nt = (; a11=1, a12=2, b1=3), we can deconstruct it by using
(; a11,a12) = nt
. However, you need to specify each variable you want to deconstruct. I think it could be worth adding some way to deconstruct all variables, or to use regular expressions for choosing the variables to deconstruct. Something along the lines(; r"^a") = nt
to geta11
anda12
into local scope.The main advantage of using NamedTuples relative to
struct
is that it complements perfectly with DataFrames (you can always convert a row of a DataFrame into a NamedTuple by usingrowtable
).The typical use I have in mind is for Economics, but it'd be useful for any kind of model. For instance, right now I'm working with a dataframe encompassing hundreds of industries. Since it requires a lot of previous transformations, using DataFrames is a must. But, then, I compute a model that uses information for each industry and makes use of tons of variables for each industry. So, it's easy to convert each industry info into a NamedTuple (instead of working with a
struct
where you have to manually specify the type, name, etc).However, you need to manually add the variables that you deconstruct for every function in the model. The same is true for the parameters of the model. Right now,
Parameters.jl
allows you to deconstruct all variables in astruct
, but this is not possible for NamedTuples.It seems other users also thought this feature could be useful. See here and here.
And from what I understand from #39285, it's a feature that could be added? (@simeonschaub and @CameronBieganek maybe already explored this?)
Thanks!!