Mercurial > repos > public > sbplib_julia
changeset 736:b5d9fbcdcef1 feature/static_dict
Add a constuctor taking a tuple
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 17 Mar 2021 21:45:59 +0100 |
parents | 3e0b0b44868e |
children | ca4f17efb279 |
files | src/StaticDicts/StaticDicts.jl test/testStaticDicts.jl |
diffstat | 2 files changed, 4 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/StaticDicts/StaticDicts.jl Wed Mar 17 21:29:37 2021 +0100 +++ b/src/StaticDicts/StaticDicts.jl Wed Mar 17 21:45:59 2021 +0100 @@ -23,12 +23,10 @@ struct StaticDict{K,V,N} <: AbstractDict{K,V} pairs::NTuple{N,Pair{K,V}} - # TBD: Why doesn't `pairs::NTuple{N,Pair{K,V}}` work? function StaticDict{K,V,N}(pairs::Tuple) where {K,V,N} if !allunique(first.(pairs)) throw(ArgumentError("keys must be unique (for now)")) end - return new{K,V,N}(pairs) end end @@ -40,6 +38,8 @@ return StaticDict{K,V,N}(pairs) end +StaticDict(pairs::NTuple{N,Pair} where N) = StaticDict(pairs...) + function Base.get(d::StaticDict, key, default) for p ∈ d.pairs # TBD: Is this the best? Should we use the iterator on `d`? if key == p.first
--- a/test/testStaticDicts.jl Wed Mar 17 21:29:37 2021 +0100 +++ b/test/testStaticDicts.jl Wed Mar 17 21:45:59 2021 +0100 @@ -12,6 +12,8 @@ @test d[1] == 2 @test d[3] == 4 + @test StaticDict((1=>2, 3=>4)) == d + @test StaticDict(1=>3, 2=>4.) isa StaticDict{Int,Real} @test StaticDict(1. =>3, 2=>4) isa StaticDict{Real,Int} @test StaticDict(1. =>3, 2=>4.) isa StaticDict{Real,Real}