On Tue, Dec 15, 2015 at 4:17 PM, <[email protected]> wrote:
> This works and makes perfect sense, thank you. May I bother you one more
> time? I want to generate these functions for a lot of operators, I create an
> Array containing them all and I loop over them to apply the macro:
The macro expansion happens before the loop runs so no.
If you want to do this in a loop you should use `eval`. Either the
version Erik mentioned earlier or you can evaluate your macro call as
well
Sth like `@eval @operator_obs $o` should work.
>
> macro operator_obs(name)
> dname = symbol(".$name")
> return quote
> function $(esc(name))(m1::M, m2::M)
> return M($(esc(dname))(m1.a, m2.a), $(esc(dname))(m1.b, m2.b))
> end
> end
> end
>
> ol = [+, -, *, /, ^]
>
> for o in ol
> @operator_obs(o)
> end
>
> This won't work anymore because Julia complains that ERROR: LoadError:
> UndefVarError: .o not defined, which I understand. Is there any way to
> circumvent that?