通过编程生成的功能,如何定义DOC?

发布于 2025-02-08 05:36:28 字数 1423 浏览 2 评论 0 原文

我想按照但是有一些文档。

最小可复制的例子:

julia> cases = ( :A => 1, :B => 2 )
(:A => 1, :B => 2)

julia> for (name,value) in cases
           func_name = Symbol("foo_"*string(name))
           eval(quote
                    $func_name() = $value
                end)
       end

julia> foo_A()
1

julia> foo_B()
2

我的问题是我还想定义一些相关的文档:

help?> foo_A
search: foo_A foo_B unsafe_pointer_to_objref

  No documentation found.

  foo_A is a Function.

  # 1 method for generic function "foo_A":
  [1] foo_A() in Main at REPL[2]:4

我可以做:

for (name,value) in cases
    func_name = Symbol("foo_"*string(name))
    eval(quote
             " The function doc is here "
             $func_name() = $value
         end)
end

在这种情况下:

help?> foo_A()
  The function doc is here

但是我真正需要的是某种替代

julia> for (name,value) in cases
           func_name = Symbol("foo_"*string(name))
           eval(quote
                    " The function $name returns $value"
                    $func_name() = $value
                end)
       end
ERROR: UndefVarError: name not defined

是否可以 ?

I want to define some functions as described in the official doc but with some documentation.

Minimal reproducible example :

julia> cases = ( :A => 1, :B => 2 )
(:A => 1, :B => 2)

julia> for (name,value) in cases
           func_name = Symbol("foo_"*string(name))
           eval(quote
                    $func_name() = $value
                end)
       end

julia> foo_A()
1

julia> foo_B()
2

My problem is that I want to also define some associated doc :

help?> foo_A
search: foo_A foo_B unsafe_pointer_to_objref

  No documentation found.

  foo_A is a Function.

  # 1 method for generic function "foo_A":
  [1] foo_A() in Main at REPL[2]:4

I can do :

for (name,value) in cases
    func_name = Symbol("foo_"*string(name))
    eval(quote
             " The function doc is here "
             $func_name() = $value
         end)
end

in that case :

help?> foo_A()
  The function doc is here

But what I really need is some kind of substitution :

julia> for (name,value) in cases
           func_name = Symbol("foo_"*string(name))
           eval(quote
                    " The function $name returns $value"
                    $func_name() = $value
                end)
       end
ERROR: UndefVarError: name not defined

Is it possible ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的往事 2025-02-15 05:36:28

使用 @doc 宏:

for (name,value) in cases
    func_name = Symbol("foo_"*string(name))
    docstr = " The function $name returns $value"
    eval(quote
        @doc $docstr $func_name() = $value
    end)
end

现在哟可以按预期做:

julia> foo_A(), foo_B()
(1, 2)

help?> foo_A
search: foo_A foo_B unsafe_pointer_to_objref

  The function A returns 1

help?> foo_B
search: foo_B unsafe_pointer_to_objref foo_A

  The function B returns 2

Use the @doc macro:

for (name,value) in cases
    func_name = Symbol("foo_"*string(name))
    docstr = " The function $name returns $value"
    eval(quote
        @doc $docstr $func_name() = $value
    end)
end

Now yo can do as expected:

julia> foo_A(), foo_B()
(1, 2)

help?> foo_A
search: foo_A foo_B unsafe_pointer_to_objref

  The function A returns 1

help?> foo_B
search: foo_B unsafe_pointer_to_objref foo_A

  The function B returns 2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文