简体   繁体   English

如何在 elixir 中输入规格符号?

[英]How to typespec sigils in elixir?

the code was edited after the answer of Aleksei Matiushkin to include the @type在 Aleksei Matiushkin 的回答之后编辑了代码以包含 @type

I'm trying to typespec a sigil specifically a Regex sigil but the current elixir doc don't specify how.我正在尝试指定一个印记,特别是一个正则表达式印记,但当前的typespec文档没有指定如何。

My code is close of:我的代码接近:

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 is just syntactic sugar for sigil_Z macro, one even might define their own sigils, so their types are implementation-specific. Sigil ~Z只是sigil_Z宏的语法糖,甚至可以定义自己的 sigils,因此它们的类型是特定于实现的。

Both ~r and ~R sigils return the escaped result of a call to Regex.compile!/2 which is apparently Regex.t() . ~r~R sigils 都返回调用Regex.compile!/2 的转义结果,这显然是Regex.t()


Sidenote: your code would not compile.旁注:您的代码无法编译。 You probably wanted to define @spec fot the function as您可能想将@spec的 @spec 定义为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM