I haven't been able to figure out how to make it work inside a for loop 
though:

julia> for op in [+, -]
           @foo_op op
       end
ERROR: UndefVarError: op not defined
 [inlined code] from none:5
 in anonymous at no file:0

julia> macro foo_op(op)
           quote
               dot_op = symbol(".$($op)")
               docstr = "`$($op)` also works for `Foo` types!"
               @show $op, typeof($op)    # seems rigth to me!
               function Base.$op(a::Foo, b::Foo)
                   Foo(dot_op(a.x, b.x))
               end
               @doc Markdown.parse(docstr) Base.$op
               Base.$op
           end
       end


julia> for op in [+, -]
           @foo_op op
       end
(op,typeof(op)) = (+,Function)
ERROR: UndefVarError: op not defined
 [inlined code] from none:6
 in anonymous at no file:0



El martes, 15 de diciembre de 2015, 10:28:53 (UTC-6), [email protected] 
escribió:
>
> Hello,
>
> I have a type M and I would like to redefine all the operations *, +, -, / 
> etc. over it. Since it would be everytime the same operations and since I 
> want my code to be concise I thought about using a macro to define them, 
> such as:
>
> type M
>     a
>     b
> end
>
> macro operator_obs(name)
>     return quote
>         function $name(m1::M, m2::M)
>             return M($name(m1.a, m2.a), $name(m1.b, m2.b))
>         end
>     end
> end
>
> @operator_obs(+)
>
> m1 = M(2.0,3.0)
> m2 = M(4.0,5.0)
>
> +(m1, m2)
>
> But this doesn't seem to work. What did I do wrong?
>
> Many thanks,
>

Reply via email to