Skip to content

Commit

Permalink
fix(show)!: produce executable string for Options
Browse files Browse the repository at this point in the history
Re-implement two-argument Base.show() such that it produces an executable string of Optim.Options() as specified in Base docs [1].

Move previous implementation to three-argument Base.show() which should be human-readable [2].

[1] https://docs.julialang.org/en/v1/base/io-network/#Base.show-Tuple{IO,%20Any}
[2] https://docs.julialang.org/en/v1/base/io-network/#Base.show-Tuple{IO,%20Any,%20Any}

Refs: #915
  • Loading branch information
gwater committed Jan 20, 2022
1 parent cdebfca commit fb65f9e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,19 @@ function Options(;
Int(show_every), callback, Float64(time_limit))
end

_show_helper(output, k, v) = output * "$k = $v, "
_show_helper(output, k, ::Nothing) = output

function Base.show(io::IO, o::Optim.Options)
content = foldl(fieldnames(typeof(o)), init = "Optim.Options(") do output, k
v = getfield(o, k)
return _show_helper(output, k, v)
end
print(io, content)
println(io, ")")
end

function Base.show(io::IO, ::MIME"text/plain", o::Optim.Options)
for k in fieldnames(typeof(o))
v = getfield(o, k)
if v isa Nothing
Expand Down

0 comments on commit fb65f9e

Please sign in to comment.