-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SparseArrayDOKs] Add setindex_maybe_grow! and macro @maybe_grow #1434
Conversation
Ok just made those improvements to the |
@emstoudenmire I think we could simplify the macro implementation using a package like MacroTools.jl. There are other similar packages but I think that one is popular. |
That package looks great – I'll give it a spin, and this has been a good "basic macro practice" for me. I was hesitant to add a new dependency for this PR but if you think we would use that package in other parts of NDTensors (probably so?) then I'll add it. |
It's a lightweight and widely used package so if it simplifies the code then I don't see any harm. |
Ok, made that change. The |
There is the MacroTools.isexpr function, but the documentation for it is rather minimal so I'm not sure if it could be used. |
In the Pattern Matching section it says that:
and then suggests syntax for catching cases when the expression is not of the correct form. It looks like |
I was also hoping initially that
(it is always that error for any expression not of the form a[I...] = v). |
Maybe we could wrap it in a |
Oh, that's a good idea. I'll do that. |
Unfortunately I am not able to get the macro maybe_grow(ex)
try
@capture(ex, array_[indices__] = value_)
return :(setindex_maybe_grow!($(esc(array)), $value, $indices...))
catch
error(
"@maybe_grow must be used with setindex! syntax (as @maybe_grow a[i,j,...] = value)"
)
end
end |
That's too bad, I guess it requires some more investigation. Let's at least refactor the check into a function |
I ended up blending the ideas of a checking function and using a try..catch block. The reason is that there are some expressions (for example Also apparently the |
Codecov ReportAll modified and coverable lines are covered by tests ✅
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## main #1434 +/- ##
==========================================
- Coverage 60.83% 59.52% -1.31%
==========================================
Files 147 139 -8
Lines 9661 9005 -656
==========================================
- Hits 5877 5360 -517
+ Misses 3784 3645 -139 ☔ View full report in Codecov by Sentry. |
You should be able to catch that kind of issue in a more elegant way using dispatch by specializing on whether or not the type of the input is 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 Note that I'm using |
That is a nicer pattern, especially since the type constraints guarantee the code inside can't error. I just updated the code to use it. MacroTools uses a lot of similar patterns internally also. |
Looks like a few more constructors need to be defined. |
Avoid where syntax Co-authored-by: Matt Fishman <[email protected]>
Thanks! This will be helpful. |
Adds the
setindex_maybe_grow!
function and the macro@maybe_grow
as outlined in Issue #1433.Fixes #1433.