我发现为导出列出每个函数有点乏味。这样做(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
一些额外的上下文:
- 这个痛点通常在与
Documenter.jl
的代码/文档接口(interface)中。我正在用这两种方式写作,然后意识到“我忘了导出一些东西”。 - 围绕这个 for type fields going back to 2015 进行了很多讨论. Private properties in types/structs有一些最佳实践,并且 style guide was updated ~25 days ago使这一点更明确。不过,我找不到类似功能的东西。
编辑:提出一个可能的折衷方案: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
关于design-patterns - 在 Julia 模块中自动导出函数的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67548496/