diff --git a/src/ordered_dict.jl b/src/ordered_dict.jl index d08e715..72b2596 100644 --- a/src/ordered_dict.jl +++ b/src/ordered_dict.jl @@ -67,15 +67,20 @@ OrderedDict(kv::AbstractDict{K,V}) where {K,V} = OrderedDict{K,V}(kv) OrderedDict(ps::Pair{K,V}...) where {K,V} = OrderedDict{K,V}(ps) OrderedDict(ps::Pair...) = OrderedDict(ps) -function OrderedDict(kv) - try - dict_with_eltype((K, V) -> OrderedDict{K, V}, kv, eltype(kv)) - catch e - if isempty(methods(iterate, (typeof(kv),))) || - !all(x->isa(x, Union{Tuple,Pair}), kv) - throw(ArgumentError("OrderedDict(kv): kv needs to be an iterator of tuples or pairs")) - else - rethrow(e) +@static if VERSION >= v1.11 + # see JuliaLang/julia#53151 + OrderedDict(kv) = dict_with_eltype((K, V) -> OrderedDict{K, V}, kv, eltype(kv)) +else + function OrderedDict(kv) + try + dict_with_eltype((K, V) -> OrderedDict{K, V}, kv, eltype(kv)) + catch e + if isempty(methods(iterate, (typeof(kv),))) || + !all(x->isa(x, Union{Tuple,Pair}), kv) + throw(ArgumentError("OrderedDict(kv): kv needs to be an iterator of tuples or pairs")) + else + rethrow(e) + end end end end diff --git a/test/test_ordered_dict.jl b/test/test_ordered_dict.jl index 18f7306..a7551a0 100644 --- a/test/test_ordered_dict.jl +++ b/test/test_ordered_dict.jl @@ -507,6 +507,13 @@ using OrderedCollections, Test @test del_slots4 == 0 end + @testset "Issue #86" begin + counter = 0 + expensive_function(k) = (counter += 1; k > 2 && error("too large!")) + @test_throws ErrorException OrderedDict(k => expensive_function(k) for k in 1:3) + @test counter == 3 + end + @testset "ordered access" begin od = OrderedDict(:a=>1, :b=>2, :c=>3) @test popfirst!(od) == (:a => 1)