Skip to content

Commit

Permalink
Merge pull request #187 from aplavin/accessor-macro
Browse files Browse the repository at this point in the history
fix trivial @Accessor functions
  • Loading branch information
jw3126 authored Dec 24, 2024
2 parents 97c4aa7 + 516efd7 commit 2675bd3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/sugar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ function parse_obj_optics(ex)
obj, frontoptic = parse_obj_optics(args[2])
optic = :(Base.Fix1($f, $(esc(args[1]))))
end
elseif @capture(ex, s_Symbol)
# the symbol can be wrapped in quote ... end, need to unwrap for reliable handling downstream
obj = esc(s)
return obj, ()
else
obj = esc(ex)
return obj, ()
Expand Down
2 changes: 2 additions & 0 deletions test/test_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ struct MyStruct
end
"Documentation for my_x"
@accessor my_x(v) = v.x
@accessor my_identity(v) = v
@accessor Base.:(+)(s::MyStruct) = 5 - s.x.a
@accessor Base.Int(s::MyStruct) = s.x[1]
@accessor Base.Float64(s::MyStruct) = s.x[2]
Expand All @@ -617,6 +618,7 @@ import REPL
@test (@set my_x(s) = 456) === MyStruct(456)
@test (@set +s = 456) === MyStruct((a=5-456,))
test_getset_laws(my_x, s, 456, "1")
test_getset_laws(my_identity, 123, 456, "1")
test_getset_laws(+, s, 456, 1.0)

s = MyStruct((1, 2.0))
Expand Down

0 comments on commit 2675bd3

Please sign in to comment.