Skip to content

Commit

Permalink
@reset immediately throws if the target isn't a symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Dec 27, 2023
1 parent 5ba9aff commit 12533f8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
13 changes: 11 additions & 2 deletions src/sugar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ function setmacro(optictransform, ex::Expr; overwrite::Bool=false)
f = :($_UpdateOp($op,$val))
:($modify($f, $obj, ($optictransform)($optic)))
end
return overwrite ? :($obj = $ret) : ret
return _macro_expression_result(obj, ret; overwrite=overwrite)
end

"""
Expand Down Expand Up @@ -336,9 +336,18 @@ function insertmacro(optictransform, ex::Expr; overwrite::Bool=false)
obj, optic = parse_obj_optic(ref)
val = esc(val)
ret = :($insert($obj, ($optictransform)($optic), $val))
return overwrite ? :($obj = $ret) : ret
return _macro_expression_result(obj, ret; overwrite=overwrite)
end

_macro_expression_result(obj, ret; overwrite) =
if overwrite
@assert Meta.isexpr(obj, :escape)
only(obj.args) isa Symbol || throw(ArgumentError("Rebinding macros can only be used with plain variables as targets. Got expression: $obj"))
return :($obj = $ret)
else
return ret
end

"""
@optic
Expand Down
18 changes: 11 additions & 7 deletions test/test_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ end
nt = (a=1,)
@reset nt.a = 5
@test nt === (a=5,)

@test_throws Exception eval(:(@reset func(x, y) = 100))
end

@testset "@set" begin
Expand Down Expand Up @@ -142,14 +144,16 @@ end
x_orig = x
@test (@set $(x)[2] = 100) == [1, 100, 3]
@test (@set $(x[2]) = 100) == 100
@test (@set $(x)[2] + 2 = 100) == [1, 98, 3] # impossible without $
@test (@set $(x[2]) + 2 = 100) == 98 # impossible without $
@test x_orig === x == [1, 2, 3]
# these are impossible with @set without $:
@test (@set $(x)[2] + 2 = 100) == [1, 98, 3]
@test (@set $(x[2]) + 2 = 100) == 98
@test (@set first($x, 2) = [10, 20]) == [10, 20, 3]

@test (@reset $(x[2]) = 100) == 100
@test x_orig === x == [1, 100, 3]
@test (@reset $(x)[2] = 200) == [1, 200, 3]
@test x_orig !== x == [1, 200, 3]
@test_throws Exception eval(:(@reset $(x[2]) = 100))
@test_throws Exception eval(:(@reset $(x)[2] = 200))

# ensure the object itself didn't change:
@test x_orig === x == [1, 2, 3]
end


Expand Down

0 comments on commit 12533f8

Please sign in to comment.