Hello,

I'm new to Julia metaprogramming but I had a look at both
http://docs.julialang.org/en/release-0.5/manual/metaprogramming/
and
http://docs.julialang.org/en/release-0.5/manual/documentation/

I'd like to define (using metaprogramming) 4 functions (add, subtract, 
multiply, divide)
with their associated operator and docstring

I also would like to be able to "inspect" high level code... which is quite 
close
to what was asked in https://github.com/JuliaLang/julia/issues/2625

So here is what I did:

D_FUNC = Dict(
    :add => (:+, "`add(a,b)` adds `a` and `b` together"),
    :subtract => (:-, "`subtract(a,b)` subtracts `b` from `a`"),
    :multiply => (:*, "`multiply(a,b)` multiplies `b` from `a`"),
    :divide => (:/, "`divide(a,b)` divides `b` from `a`")
)

_D_CODE = Dict{Symbol,Expr}()

for (f, (op, s_doc)) in D_FUNC
    expression = quote
        $f(a,b) = $op(a,b)
    end
    _D_CODE[f] = expression
    eval(expression)
end

This is quite close to my goals.

I can see generated code

julia> _D_CODE[:add]
quote  # REPL[3], line 3:
    add(a,b) = begin  # REPL[3], line 3:
            a + b
        end
end

Functions are defined correctly:

julia> add(4,3)
7

julia> multiply(4,3)
12

but I don't know how I can "attach" docstring to each function (inside the 
for loop)

Any help will be great.

Kind regards


PS: Remy's answer in this StackOverflow question 
http://stackoverflow.com/questions/36191766/metaprogramming-within-docstring-with-eval
 
might be edited
because it doesn't work anymore with Julia 0.5

Reply via email to