【发布时间】:2021-05-15 15:41:30
【问题描述】:
我觉得列出每个函数 来导出有点乏味。这样做(几年前的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 的公共/私有方法的语法做一个类比:
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的代码/文档界面中。我在两者中都写了,并意识到“我忘了导出一些东西”。 - 围绕这个for type fields going back to 2015 进行了很多讨论。 Private properties in types/structs 有一些最佳实践,而style guide was updated ~25 days ago 可以更明确地说明这一点。不过,我找不到与函数类似的东西。
编辑:提出可能的妥协:ExportPublic.jl
【问题讨论】:
标签: design-patterns module julia