Skip to content

Commit e20ab52

Browse files
author
Miha Zgubic
committed
use a macro to get around constructors
1 parent 972f34c commit e20ab52

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/to_vec.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ end
2121
# Base case -- if x is already a Vector{<:Real} there's no conversion necessary.
2222
to_vec(x::Vector{<:Real}) = (x, identity)
2323

24+
# get around the outer constructors
25+
macro _force_construct(T, argsplat_ex)
26+
@assert argsplat_ex.head == :(...)
27+
args = first(argsplat_ex.args)
28+
return esc(Expr(:splatnew, T, args))
29+
end
30+
2431
# Fallback method for `to_vec`. Won't always do what you wanted, but should be fine a decent
2532
# chunk of the time.
2633
function to_vec(x::T) where {T}
@@ -36,9 +43,9 @@ function to_vec(x::T) where {T}
3643
val_vecs = vals_from_vec(v)
3744
values = map((b, v) -> b(v), backs, val_vecs)
3845
try
39-
return T(values...)
46+
T(values...)
4047
catch MethodError
41-
return T.name.wrapper(values...)
48+
return @_force_construct(T, values...)
4249
end
4350
end
4451
return v, structtype_from_vec

0 commit comments

Comments
 (0)