Skip to content

Commit

Permalink
Use dispatch to check setindex! expression
Browse files Browse the repository at this point in the history
  • Loading branch information
emstoudenmire committed May 14, 2024
1 parent 0520e67 commit 7da8f09
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions NDTensors/src/lib/SparseArrayDOKs/src/sparsearraydok.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,26 @@ function check_siteindex!_expr(expr, macroname=:macro)
try
@assert expr.head == :(=) && expr.args[1] isa Expr && expr.args[1].head == :(ref)
catch
error(
"$macroname must be used with setindex! syntax (as @$macroname a[i,j,...] = value)"
)
end
end

function is_setindex!_expr(expr::Expr)
return is_assignment_expr(expr) && is_getindex_expr(first(expr.args))
end
is_setindex!_expr(x) = false

is_getindex_expr(expr::Expr) = (expr.head === :ref)
is_getindex_expr(x) = false

is_assignment_expr(expr::Expr) = (expr.head === :(=))
is_assignment_expr(expr) = false

macro maybe_grow(expr)
check_siteindex!_expr(expr, :maybe_grow)
if !is_setindex!_expr(expr)
error(
"@maybe_grow must be used with setindex! syntax (as @maybe_grow a[i,j,...] = value)"
)
end
@capture(expr, array_[indices__] = value_)
return :(setindex_maybe_grow!($(esc(array)), $value, $indices...))
end

0 comments on commit 7da8f09

Please sign in to comment.