changeset 720:172c55c4cf2e feature/static_dict

Dissalow duplicate keys
author Jonatan Werpers <jonatan@werpers.com>
date Tue, 16 Mar 2021 22:35:40 +0100
parents 2f8c67c5979e
children b2af21a4b376
files src/StaticDicts/StaticDicts.jl test/testStaticDicts.jl
diffstat 2 files changed, 13 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/StaticDicts/StaticDicts.jl	Tue Mar 16 17:28:40 2021 +0100
+++ b/src/StaticDicts/StaticDicts.jl	Tue Mar 16 22:35:40 2021 +0100
@@ -16,6 +16,15 @@
 """
 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
 
 function StaticDict(pairs::Vararg{Pair})
--- a/test/testStaticDicts.jl	Tue Mar 16 17:28:40 2021 +0100
+++ b/test/testStaticDicts.jl	Tue Mar 16 22:35:40 2021 +0100
@@ -15,6 +15,9 @@
         @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}
+
+        @test_throws ArgumentError StaticDict(1=>3, 1=>3)
+        #TBD: is ArgumentError correct?
     end
 
     @testset "equality" begin
@@ -35,7 +38,7 @@
             StaticDict(3=>5,4=>6)) == StaticDict(
                 1=>3, 2=>4, 3=>5, 4=>6
             )
-        @test_broken merge(d,d) == d # Should this be valid?
+        @test_throws ArgumentError merge(StaticDict(1=>3),StaticDict(1=>3))
     end
 end