Skip to content

NamedTuple unpacking (and also struct unpacking) #28579

@mauro3

Description

@mauro3

During JuliaCon discussion with @ahojukka5, I figured it would be cool to have NamedTuple unpacking in function arguments which honors the names of the named tuple. Currently we have:

julia> nt = (a=1, b=2)

julia> f((b,)) = b                                                                                                                                        
f (generic function with 1 method)                                                                                                                          

julia> f(nt)                                                                                                                                                 
1                                                                                                                                                            

instead, I'd think this would be more intuitive:

julia> f(nt)                                                                                                                                                 
2

Similarly it could work for structs:

julia> struct A; a; b end                                                                                                                                    
                                                                                                                                                             
julia> aa = A(1,2)                                                                                                                                            
A(1, 2)                                                                                                                                                      
                                                                                                                                                             
julia> f(aa)                                                                                                                                                  
2

(Unfortunately, this needs a 2.0 label)

Edit: fixed unneeded/confusing default-value method-defs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions