JeffreySarnoff / NamedTupleTools.jl

some utilities for working with NamedTuples
MIT License
80 stars 11 forks source link

better living through software #20

Open JeffreySarnoff opened 4 years ago

JeffreySarnoff commented 4 years ago

Hi @cscherrer @KristofferC @kmsquire @pdeffebach @scls19fr @sethaxen @tkf.

I've been working on NamedTupleTools#v1, intending to provide a solid, effective, consistent, performant, robust and comfortable API. The #v1 README is currentish with the source code.

Your thoughts, insights, perspectives, and suggestions are most welcome.

Best, Jeffrey Sarnoff

tkf commented 4 years ago

For prototype, have a look at ConstructionBase.constructorof. I think it is more efficient and has more sane behavior:

julia> t = NamedTuple{(:a,)}
NamedTuple{(:a,),T} where T<:Tuple

julia> t(1)
(a = 1,)

julia> t((1,))
(a = 1,)

julia> t(((1,),))
(a = (1,),)

julia> c = constructorof(typeof((a=1,)))
ConstructionBase.NamedTupleConstructor{(:a,)}()

julia> c(1)
(a = 1,)

julia> c((1,))
(a = (1,),)

ConstructionBase.jl also provides setproperties which can be used as setproperties(nt; :b => "cat") instead of setproperty(nt, :b, "cat"). For resetproperty, why not just do (; nt..., :b => "cat")?