我更喜欢处理少于1000行的文件,所以我想把一些Erlang模块分成更小的部分。
有没有一种方法可以在不扩展库的公共API的情况下实现这一点?
我的意思是,任何时候有一个模块,任何用户都可以做module:func_exported_from_the_module
。唯一的方法,真正有东西是私人的,我知道是不导出它从任何模块(即使这样,洞可以戳)。
那么,如果技术上没有办法实现我所寻找的,有没有约定呢?例如,Python类中没有private方法,但是约定是在_my_private_method
中使用前导_
来标记它为private。
我接受的答案可能是,“不,你必须有4K LOC文件。”
4条答案
按热度按时间blmhpbnm1#
最接近约定的是使用edoc标签,如
@private
和@hidden
。来自文档:
@hidden
标记函数,使其不会出现在文档中(即使生成了"私有"文档)。对调试/测试函数等有用。内容可用作注解;它将被EDoc忽略。
@private
将函数标记为private(即不是public接口的一部分),这样它就不会出现在普通文档中。(如果生成了"private"文档,则会包含该函数。)仅对导出函数有用,例如spawn的入口点。(非导出函数总是"private"。)内容可以用作注解;它将被EDoc忽略。
ego6inou2#
Different visibilities for different methods is not currently supported.
The current convention, if you want to call it that way, is to have one (or several) 'facade'
my_lib.erl
module(s) that export the public API of your library/application. Calling any internal module of the library is playing with fire and should be avoided (call them at your own risk).There are some very nice features in the BEAM VM that rely on being able to call exported functions from any module, such as
Module:
) to the most recent version and the internal calls to the current version. That's why you may see some?MODULE:
calls in long-running servers, to be able to upgrade the codeYou'd be able to argue that these points'd be available with more fine-grained BEAM-oriented visibility levels, true. But I don't think it would solve anything that's not solved with the facade modules, and it'd complicate other parts of the VM/code a great deal.
Bonus
Something similar applies to records and opaque types, records only exist at compile time, and opaque types only at dialyzer time. Nothing stops you from accessing their internals anywhere, but you'll only find problems if you go that way:
{record_name,...} =
breakopaque_adt()
, you know that it's a list and use like so. The library is upgraded to include the size of the list, so nowopaque_adt()
is atuple()
and chaos ensuesdy1byipe3#
只有那些在
-export
属性中指定的函数对其他模块可见,即“public”函数。所有其他函数都是private。如果您只指定了-compile(export_all)
,则模块中的所有函数在外部都是可见的。不建议使用-compile(export_all)
。7dl7o3gd4#
我不知道Erlang有什么现有的约定,但是为什么不采用Python的约定呢?假设“library-private”函数的前缀是下划线。你需要用单引号将函数名引起来才能做到这一点:
那么你可以称之为:
对我来说,这清楚地传达了我不应该调用这个函数,除非我知道我在做什么。