Skip to content
This repository has been archived by the owner on Jun 4, 2019. It is now read-only.

Commit

Permalink
Add NamedTuple constructor for iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
omus committed Apr 27, 2018
1 parent 72ca946 commit 381d926
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/NamedTuples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 381d926

Please sign in to comment.