On Tue, Dec 15, 2015 at 4:50 PM, <[email protected]> wrote: > Thank you Yichao, you have been of inestimable help. Everything works up to > one detail (that I do not necessarily need to solve, but just for the > record): > > macro operator_obs(name) > nname = symbol("$name") > dname = symbol(".$name") > return quote > function $(esc(nname))(m1::M, m2::M) > return M($(esc(dname))(m1.a, m2.a), $(esc(dname))(m1.b, m2.b)) > end > end > end > > ol = [+, -, *, /, ^]
Use `ol = [:+, :-, :*, :/, :^]` instead (you might need parenthesis, e.g. `:(+)` for some symbol, (e.g. =, or == IIRC)) You are splicing the element in the AST and you want to splice the symbol and not the function object. print the type of `name` in the macro to see the difference. Also, unless you are only using this macro on a function you already import to the module that use this macro, you'd better fully quantify the name instead. `Base.$(name)` (the esc should not be necessary here) Ref https://github.com/andrewcooke/AutoHashEquals.jl/pull/1/files > > for o in ol > @eval @operator_obs($o) > end > > That's the final solution, the purpose of nname in the macro is because, for > some obscure reason, Julia complains if I keep using $(esc(name)) as the > function name, although I can use it inside the macro everywhere else. And > it works if I try with dot operators like .+... I do not quite understand > that part to be honest.
