Skip to content

Commit

Permalink
show optics as @o (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin authored Feb 28, 2024
1 parent e0ab153 commit 48fa640
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions docs/src/lenses.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ julia> struct T;a;b; end
julia> obj = T("AA", "BB");
julia> lens = @optic _.a
(@optic _.a)
(@o _.a)
julia> lens(obj)
"AA"
Expand All @@ -36,7 +36,7 @@ julia> v = (a = 1:3, )
(a = 1:3,)
julia> l = opcompose(PropertyLens(:a), IndexLens(1))
(@optic _.a[1])
(@o _.a[1])
julia> l ≡ @optic _.a[1] # equivalent to macro form
true
Expand Down
6 changes: 3 additions & 3 deletions src/optics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ julia> la = @optic _.a
lb = @optic _.b
lc = @optic _.c
lens = la ⨟ lb ⨟ lc
(@optic _.c) ∘ (@optic _.a.b)
(@o _.c) ∘ (@o _.a.b)
julia> lens(obj)
1
Expand Down Expand Up @@ -210,7 +210,7 @@ end
Elements
Access all elements of a collection that implements `map`.
An alias for `Elements()` is available as `∗` (`\\ast`). This optic can also be written as `@optic _[∗]`.
An alias for `Elements()` is available as `∗` (`\\ast`). This optic can also be written as `@o _[∗]`.
```jldoctest
julia> using Accessors
Expand Down Expand Up @@ -297,7 +297,7 @@ end
Properties()
Access all properties of an object.
An alias for `Properties()` is available as `∗ₚ` (`\\ast\\_p`). This optic can also be written as `@optic _[∗ₚ]`.
An alias for `Properties()` is available as `∗ₚ` (`\\ast\\_p`). This optic can also be written as `@o _[∗ₚ]`.
```jldoctest
julia> using Accessors
Expand Down
21 changes: 16 additions & 5 deletions src/sugar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,14 @@ _macro_expression_result(obj, ret; overwrite) =
end

"""
@optic
@optic expr
@o expr
Construct an optic from property access and similar.
Construct an optic from an expression containing property/index access, function calls, and more.
Inside the macro, `_` is used as the placehold to refer to the target object.
The two forms, `@optic` and `@o`, are equivalent. We recommend using `@o` for brevity unless there is a potential for confusion.
# Example
Expand All @@ -364,7 +369,7 @@ julia> t = T("A1", T(T("A3", "B3"), "B2"))
T("A1", T(T("A3", "B3"), "B2"))
julia> l = @optic _.b.a.b
(@optic _.b.a.b)
(@o _.b.a.b)
julia> l(t)
"B3"
Expand All @@ -375,8 +380,14 @@ T("A1", T(T("A3", 100), "B2"))
julia> t = ("one", "two")
("one", "two")
julia> set(t, (@optic _[1]), "1")
julia> set(t, (@o _[1]), "1")
("1", "two")
julia> l = @o _[∗].a
(@o _[∗].a)
julia> modify(x -> x + 1, ((a=1,), (a=2,)), l)
((a = 2,), (a = 3,))
```
See also [`@set`](@ref).
Expand Down Expand Up @@ -484,7 +495,7 @@ function show_optic(io, optic)
if get(io, :compact, false)
print(io, shortstr)
else
print(io, "(@optic ", shortstr, ")")
print(io, "(@o ", shortstr, ")")
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/test_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,9 @@ end
end

@testset "full and compact show" begin
@test sprint(show, (@optic _.a)) == "(@optic _.a)"
@test sprint(show, (@optic log(_.a[2]))) == "(@optic log(_.a[2]))"
@test sprint(show, (@optic log(_).a[2])) == "(@optic _.a[2]) ∘ log" # could be shorter, but difficult to dispatch correctly without piracy
@test sprint(show, (@optic _.a)) == "(@o _.a)"
@test sprint(show, (@optic log(_.a[2]))) == "(@o log(_.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 48fa640

Please sign in to comment.