I want to use two macros (@m1(x), @m2(y)), so that the behaviour of @m2(y) would defer depending on if y = @m1(x) or not.
Is this achievable as such? Or do I need to wrap the output of @m1 in a “label” type that would then be checked for by @m2?
I want to use two macros (@m1(x), @m2(y)), so that the behaviour of @m2(y) would defer depending on if y = @m1(x) or not.
Is this achievable as such? Or do I need to wrap the output of @m1 in a “label” type that would then be checked for by @m2?
What do you mean by the behavior being different. If you are talking about macro expanding to different code then no that’s impossible. If you mean difference as in the same sense that y = 1
and y = 2
will cause y + 1
to produce different result at runtime then yes, of course.
In any case, macro is not magic. You should figure out how you’d write the code without the macro and then write a macro to generate that code with less typing if necessary.
I’m basically trying to reproduce something akin to Haskell’s {# RULES #}
, which is pretty much akin to applying a macro on a whole file.
For example nested map
call could be replaced by a unique map call:
arr2 = map(f, arr1)
arr3 = map(f, arr2)
could be rewritten as
arr3 = map(f ∘ f, arr1)
This looks like a good problem for Cassette.jl. I’m not fluent enough to see how to do this yet, but the ability to modify the code while carrying along a context seems better than trying to mark data dependencies with a macro.