From 381d926261bd2ad386c9550c4f1cf90205065ddc Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Thu, 26 Apr 2018 16:59:26 -0500 Subject: [PATCH] Add NamedTuple constructor for iterators --- src/NamedTuples.jl | 13 +++++++++++++ src/deprecated.jl | 10 ---------- test/runtests.jl | 4 ++++ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/NamedTuples.jl b/src/NamedTuples.jl index bed973b..471ecd7 100644 --- a/src/NamedTuples.jl +++ b/src/NamedTuples.jl @@ -161,6 +161,19 @@ end end +if VERSION < v"0.7.0-DEV.2738" + function (::Type{NT})(itr) where {NT <: NamedTuple} + T = isa(NT, DataType) ? Tuple{NT.parameters...} : Tuple + @show T itr + NT(T(itr)) + end +else + # TODO: Will need to be wrapped in a VERSION check when + # https://github.com/JuliaLang/julia/pull/26914 is merged + NamedTuple{names, T}(itr) where {names, T} = NamedTuple{names, T}(T(itr)) + NamedTuple{names}(itr) where {names} = NamedTuple{names}(Tuple(itr)) +end + if VERSION < v"0.7.0-DEV.2738" # Create a NameTuple type, if a type with these field names has not already diff --git a/src/deprecated.jl b/src/deprecated.jl index 5018b30..6b9697f 100644 --- a/src/deprecated.jl +++ b/src/deprecated.jl @@ -12,16 +12,6 @@ if VERSION < v"0.7.0-DEV.2738" aexprs = [ :(args[$i]) for i = 1:n ] return gen_namedtuple_ctor_body(n, aexprs) end - - # specialized for certain argument counts - for n = 0:5 - args = [ Symbol("x$n") for n = 1:n ] - @eval function (::Type{NT})($(args...)) where {NT <: NamedTuple} - Base.depwarn("`$NT(args...)` is deprecated, use `$NT((args...,))` instead.", - Symbol("NamedTuple(args...)")) - $(gen_namedtuple_ctor_body(n, args)) - end - end end # END NamedTuples 4.1.0 deprecations diff --git a/test/runtests.jl b/test/runtests.jl index d860f5f..354ce91 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -97,3 +97,7 @@ if VERSION < v"0.7.0-DEV.2738" else @test nt.parameters[2] == Tuple{Empty, Int} end + +# Iterator constructor +@test @NT(a::Int, b::Float64)(Any[1.0, 2]) === @NT(a=1, b=2.0) +@test @NT(a, b)(Any[1.0, 2]) === @NT(a=1.0, b=2)