Skip to content

Commit

Permalink
support @o val for Returns(val)
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Dec 18, 2024
1 parent 0d619a4 commit 4402cda
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/overrides.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ _esc_and_dot_name_to_broadcasted(f::Symbol) =
esc(f)
end


# changes from upstream:
# - call parse_obj_optic_underscore_only to allow `@o 123`
function Accessors.opticmacro(optictransform::Function, ex)
obj, optic = parse_obj_optic_underscore_only(ex)
if obj != esc(:_)
msg = """Cannot parse optic $ex. Lens expressions must start with _, got $obj instead."""
throw(ArgumentError(msg))
end
:($(optictransform)($optic))
end
parse_obj_optic_underscore_only(ex) =
if tree_contains(ex, :_)
Accessors.parse_obj_optic(ex)
else
esc(:_), :(Returns($(esc(ex))))
end

# changes from upstream:
# https://github.com/JuliaObjects/Accessors.jl/pull/103
# creating:
Expand Down
2 changes: 2 additions & 0 deletions test/maybe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,6 @@ end
@test osomething(@o(_.a)) === @osomething _.a
@test osomething(@o(_.a), @o(_.b)) === @osomething _.a _.b
@test osomething(@o(_.a), @o(f(_.b))) === @osomething _.a f(_.b)
@test osomething(@o(_.a), @o NaN) === @osomething _.a NaN
@test osomething(@o(_.a), @o 0/0) === @osomething _.a NaN
end

0 comments on commit 4402cda

Please sign in to comment.