在 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
一些附加上下文:
- 这个痛点通常在code/docs与
Documenter.jl
的接口。我正在写这两个并意识到“我忘记导出一些东西”。
- 围绕这个 for type fields going back to 2015. Private properties in types/structs have some best practices, and the 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
我觉得列出每个导出函数有点乏味。这样做(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
一些附加上下文:
- 这个痛点通常在code/docs与
Documenter.jl
的接口。我正在写这两个并意识到“我忘记导出一些东西”。 - 围绕这个 for type fields going back to 2015. Private properties in types/structs have some best practices, and the 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