在 Julia 模块中自动导出函数的语法

Syntax to automatically export functions in a Julia module

我觉得列出每个导出函数有点乏味。这样做(pulled from a thread 几年前)感觉很糟糕:

module DemoModule
  # 25 functions here...

  for n in names(current_module(), true)
    if Base.isidentifier(n) && n ∉ (Symbol(current_module()), :eval)
      @eval export $n
    end
  end
end

是否有语法糖标记函数自动导出?我想用 Python 的语法来类比 public/private 方法:

module DemoModule

  function add(a, b)
    # `add` is exported automatically
    return _private_helper(a) + _private_helper(b)
  end

  function _private_helper(a)
    # `_private_helper` should not be exported, so it's marked with a `_`
    return a
  end
end

一些附加上下文


编辑:提出了一个可能的折衷方案:ExportPublic.jl

这在 Julia 社区的讨论中通常被认为是不推荐的。

但是有一个包 ExportAll.jl 正是这样。

module Bar
    using ExportAll
    function foo()
        1
    end

    function bar()
        2
    end
    @exportAll()
end

现在您可以:

julia> using Main.Bar

julia> bar()
2