如何在长生不老药中输入规格印记?

How to typespec sigils in elixir?

the code was edited after the answer of Aleksei Matiushkin to include the @type

我正在尝试 typespec 一个印记,特别是一个 Regex 印记,但当前的 elixir 文档没有具体说明如何。

我的代码接近于:

defmodule M do
  @type pattern :: ??
  @type input :: String.t()
  @type current :: non_neg_integer()

  def tokenize_pattern(pattern, input, current) do
    # ...
  end
end
iex> M.tokenize_pattern(~r/[0-9]/u, "123 hello", 0)

Sigil ~Z 只是 sigil_Z 宏的语法糖,甚至可以定义自己的印记,所以它们的类型是 implementation-specific.

~r~R 印记 return the escaped result of a call to Regex.compile!/2 which is apparently Regex.t()


旁注: 您的代码无法编译。您可能想将函数的 @spec 定义为

  @spec (pattern :: Regex.t(),
         input :: String.t(),
         current :: non_neg_integer()) :: any()
  def tokenize_pattern(pattern, input, current) do
    # ...
  end
end