使用@eval 在文档字符串中进行元编程

Rem*_*elt 4 julia

我试图将文档字符串与用@eval宏定义的函数相关联;我还希望使用符号来动态生成文档字符串。

for (f, name) in ((:add, :addition), ... )
    @eval begin
        @doc "Documentation for $name" ->
        function f(args)
             ## FUNCTION BODY
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

虽然我可以$name@eval语句中成功引用,但我不能$name从文档字符串本身中引用。它给出了错误UndefVarError: name not defined

1)有没有办法让它起作用?我尝试了多种方法来摆脱@doc范围并访问周围范围内的变量,但我没有成功。

2)->语法的本质是什么?
->从 Github获得了语法,但我在 Julia 文档中找不到它的提及,即使使用 Julia 一段时间了,我以前也没有遇到过。

Rem*_*elt 6

As linked to by @jverzani, all that is needed is an additional $. One $ is needed for expression interpolation, and the other is needed for the string interpolation. The final code is as follows:

for (f, name) in ((:add, "addition"), (:sub, "subtraction"), ...)
    @eval begin
        @doc """
        This is the function $($name)
        """
        function $f()
            ## FUNCTION BODY
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

Super simple once you know the answer...