Skip to content

Commit a98868d

Browse files
committed
more consistent function/type show
Shows unqualified names for both functions (as before) and types. Nice when an optic uses a non-exported type from some module.
1 parent 15bb131 commit a98868d

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/sugar.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,17 @@ IndexLens(::Tuple{Properties}) = Properties()
493493
### nice show() for optics
494494
_shortstring(prev, o::PropertyLens{field}) where {field} = "$prev.$field"
495495
_shortstring(prev, o::IndexLens) ="$prev[$(join(repr.(o.indices), ", "))]"
496-
_shortstring(prev, o::Union{Function,Type}) = _isoperator(o) ? "$o$prev" : "$o($prev)"
497-
_shortstring(prev, o::Base.Fix1) = _isoperator(o.f) ? "$(o.x) $(o.f) $prev" : "$(o.f)($(o.x), $prev)"
498-
_shortstring(prev, o::Base.Fix2) = _isoperator(o.f) ? "$prev $(o.f) $(o.x)" : "$(o.f)($prev, $(o.x))"
496+
_shortstring(prev, o::Union{Function,Type}) = _isoperator(o) ? "$(_fT_repr(o))$prev" : "$(_fT_repr(o))($prev)"
497+
_shortstring(prev, o::Base.Fix1) = _isoperator(o.f) ? "$(o.x) $(_fT_repr(o.f)) $prev" : "$(_fT_repr(o.f))($(o.x), $prev)"
498+
_shortstring(prev, o::Base.Fix2) = _isoperator(o.f) ? "$prev $(_fT_repr(o.f)) $(o.x)" : "$(_fT_repr(o.f))($prev, $(o.x))"
499499
_shortstring(prev, o::Elements) = "$prev[∗]"
500500
_shortstring(prev, o::Properties) = "$prev[∗ₚ]"
501501

502+
# compact representation of functions and types
503+
# most notably, it deals with the module name in a consistent way: doesn't show it
504+
# by default, it's not shown for functions but shown for types, see https://github.com/JuliaLang/julia/issues/56790
505+
_fT_repr(o) = repr(o; context=:compact => true)
506+
502507
# can f be stringfied using the operator (infix) syntax?
503508
# otherwise uses regular function call syntax
504509
_isoperator(f::Function) = Base.isoperator(nameof(f))

test/test_core.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@ end
468468
@test sprint(show, (@optic Tuple(_.a[2]))) == "(@o Tuple(_.a[2]))"
469469
@test sprint(show, (@optic log(_).a[2])) == "(@o _.a[2]) ∘ log" # could be shorter, but difficult to dispatch correctly without piracy
470470
@test sprint(show, (@optic log(_.a[2])); context=:compact => true) == "log(_.a[2])"
471+
@test sprint(show, (@optic Base.tail(_.a[2])); context=:compact => true) == "tail(_.a[2])" # non-exported function
472+
@test sprint(show, (@optic Base.Fix2(_.a[2])); context=:compact => true) == "Fix2(_.a[2])" # non-exported type
471473
end
472474

473475
@testset "text/plain show" begin

0 commit comments

Comments
 (0)