Skip to content

Commit

Permalink
Removal of redundant backwards compat to_namedtuple_expr (#280)
Browse files Browse the repository at this point in the history
* improved leftover to_namedtuple_expr, fixing a bug when used with Zygote

* bumped patch version

Co-authored-by: Hong Ge <[email protected]>
  • Loading branch information
torfjelde and yebai authored Jul 18, 2021
1 parent 309583a commit b82459a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DynamicPPL"
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
version = "0.12.2"
version = "0.12.3"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
37 changes: 5 additions & 32 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,12 @@ function getargs_tilde(expr::Expr)
end
end

############################################
# Julia 1.2 temporary fix - Julia PR 33303 #
############################################
function to_namedtuple_expr(syms, vals=syms)
if length(syms) == 0
nt = :(NamedTuple())
else
nt_type = Expr(
:curly,
:NamedTuple,
Expr(:tuple, QuoteNode.(syms)...),
Expr(:curly, :Tuple, [:(Core.Typeof($x)) for x in vals]...),
)
nt = Expr(:call, :($(DynamicPPL.namedtuple)), nt_type, Expr(:tuple, vals...))
end
return nt
end

if VERSION == v"1.2"
@eval function namedtuple(
::Type{NamedTuple{names,T}}, args::Tuple
) where {names,T<:Tuple}
if length(args) != length(names)
throw(ArgumentError("Wrong number of arguments to named tuple constructor."))
end
# Note T(args) might not return something of type T; e.g.
# Tuple{Type{Float64}}((Float64,)) returns a Tuple{DataType}
return $(Expr(:splatnew, :(NamedTuple{names,T}), :(T(args))))
end
else
function namedtuple(::Type{NamedTuple{names,T}}, args::Tuple) where {names,T<:Tuple}
return NamedTuple{names,T}(args)
end
length(syms) == 0 && return :(NamedTuple())

names_expr = Expr(:tuple, QuoteNode.(syms)...)
vals_expr = Expr(:tuple, vals...)
return :(NamedTuple{$names_expr}($vals_expr))
end

#####################################################
Expand Down

0 comments on commit b82459a

Please sign in to comment.