Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to_vec should drop Bools #206

Open
vpuri3 opened this issue Jul 25, 2022 · 0 comments · May be fixed by #208
Open

to_vec should drop Bools #206

vpuri3 opened this issue Jul 25, 2022 · 0 comments · May be fixed by #208

Comments

@vpuri3
Copy link

vpuri3 commented Jul 25, 2022

ref JuliaDiff/ChainRulesTestUtils.jl#258

# Fallback method for `to_vec`. Won't always do what you wanted, but should be fine a decent
# chunk of the time.
function to_vec(x::T) where {T}
Base.isstructtype(T) || throw(error("Expected a struct type"))
isempty(fieldnames(T)) && return (Bool[], _ -> x) # Singleton types
val_vecs_and_backs = map(name -> to_vec(getfield(x, name)), fieldnames(T))
vals = first.(val_vecs_and_backs)
backs = last.(val_vecs_and_backs)
v, vals_from_vec = to_vec(vals)
function structtype_from_vec(v::Vector{<:Real})
val_vecs = vals_from_vec(v)
values = map((b, v) -> b(v), backs, val_vecs)
try
T(values...)
catch MethodError
return _force_construct(T, values...)
end
end
return v, structtype_from_vec
end

@oxinabox oxinabox linked a pull request Aug 9, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant