Elixir 如何将私有函数抽象到实用程序库中?
假设我有一堆代码,如下所示:Elixir 如何将私有函数抽象到实用程序库中?,elixir,Elixir,假设我有一堆代码,如下所示: def dirs(path, regex_dir \\ ".+") do path |> normalize_path |> do_dirs([], regex_dir) end # list of bitstrings defp normalize_path([path | rest]) when is_bitstring(path) do [path | normalize_path(rest)] end # list of ch
def dirs(path, regex_dir \\ ".+") do
path
|> normalize_path
|> do_dirs([], regex_dir)
end
# list of bitstrings
defp normalize_path([path | rest]) when is_bitstring(path) do
[path | normalize_path(rest)]
end
# list of character lists
defp normalize_path([path | rest]) when is_list(path) do
[to_string(path) | normalize_path(rest)]
end
defp normalize_path([]) do
[]
end
# bitstring
defp normalize_path(path) when is_bitstring(path) do
[path]
end
# character list
defp normalize_path(path) when is_list(path) do
[to_string(path)]
end
我想在代码的另一部分中使用normalize_路径,将normalize_路径函数抽象到实用模块或库中的最佳方法是什么?我仍然希望保持该功能只在内部使用,而不是作为公共功能使用 最好的办法可能是将这些函数抽象到一个单独的模块中,并用
@moduledoc false
将其隐藏在文档中。这些函数不会是私有的,并且库的用户仍然可以访问它们,但如果不记录它们,则表示它们不是库API的一部分
defmodule Helpers do
@moduledoc false
@doc """
You can still provide per-function docs for documenting how the code works;
these docs won't be public anyways since `@moduledoc false` hides them.
"""
def helper(...), do: ...
end
+1.这正是我在将复杂的私有函数提取到其他模块以帮助测试时使用的模式。@JoséValim我一定是从某个地方得到的,对吗?:)在实用程序模块/库中包含某些内容,但将其隐藏似乎是两个相反的目标。