Skip to content

Commit

Permalink
Move expression check to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
emstoudenmire committed May 14, 2024
1 parent 625fa27 commit 0520e67
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions NDTensors/src/lib/SparseArrayDOKs/src/sparsearraydok.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,18 @@ function setindex_maybe_grow!(a::SparseArrayDOK{<:Any,N}, value, I::Vararg{Int,N
return a
end

macro maybe_grow(ex)
if !(ex.head == :(=) && ex.args[1] isa Expr && ex.args[1].head == :(ref))
function check_siteindex!_expr(expr, macroname=:macro)
try
@assert expr.head == :(=) && expr.args[1] isa Expr && expr.args[1].head == :(ref)
catch
error(
"@maybe_grow must be used with setindex! syntax (as @maybe_grow a[i,j,...] = value)"
"$macroname must be used with setindex! syntax (as @$macroname a[i,j,...] = value)"
)
end
@capture(ex, array_[indices__] = value_)
end

macro maybe_grow(expr)
check_siteindex!_expr(expr, :maybe_grow)
@capture(expr, array_[indices__] = value_)
return :(setindex_maybe_grow!($(esc(array)), $value, $indices...))
end

0 comments on commit 0520e67

Please sign in to comment.