Skip to content

Commit

Permalink
show types within optics same as functions
Browse files Browse the repository at this point in the history
For example, show result for @o Tuple(_.a[2]):
before: "Tuple ∘ (@o _.a[2])"
with this PR: "(@o Tuple(_.a[2]))"
  • Loading branch information
aplavin committed Dec 10, 2024
1 parent 11e9dc4 commit 15bb131
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/sugar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ IndexLens(::Tuple{Properties}) = Properties()
### nice show() for optics
_shortstring(prev, o::PropertyLens{field}) where {field} = "$prev.$field"
_shortstring(prev, o::IndexLens) ="$prev[$(join(repr.(o.indices), ", "))]"
_shortstring(prev, o::Function) = _isoperator(o) ? "$o$prev" : "$o($prev)"
_shortstring(prev, o::Union{Function,Type}) = _isoperator(o) ? "$o$prev" : "$o($prev)"
_shortstring(prev, o::Base.Fix1) = _isoperator(o.f) ? "$(o.x) $(o.f) $prev" : "$(o.f)($(o.x), $prev)"
_shortstring(prev, o::Base.Fix2) = _isoperator(o.f) ? "$prev $(o.f) $(o.x)" : "$(o.f)($prev, $(o.x))"
_shortstring(prev, o::Elements) = "$prev[∗]"
Expand Down
1 change: 1 addition & 0 deletions test/test_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ end
@test sprint(show, (@optic (_.a + 1) * 2)) == "(@o (_.a + 1) * 2)"
@test sprint(show, (@optic (_.a * 2) + 1)) == "(@o (_.a * 2) + 1)"
@test sprint(show, (@optic log(_.a[2]))) == "(@o log(_.a[2]))"
@test sprint(show, (@optic Tuple(_.a[2]))) == "(@o Tuple(_.a[2]))"
@test sprint(show, (@optic log(_).a[2])) == "(@o _.a[2]) ∘ log" # could be shorter, but difficult to dispatch correctly without piracy
@test sprint(show, (@optic log(_.a[2])); context=:compact => true) == "log(_.a[2])"
end
Expand Down

0 comments on commit 15bb131

Please sign in to comment.