【问题标题】:What does property .t means on elixir?属性 .t 在长生不老药上意味着什么?
【发布时间】:2018-05-29 10:33:48
【问题描述】:

例如,在这个模块字符串的类型规范中:

@spec validate(String.t) :: {:atom}

这是什么意思?以及如何通过iex进行测试?

** (UndefinedFunctionError) function String.t/0 is undefined or private
    (elixir) String.t()

更新

看起来有些人没有看到通过 iex 进行测试的必要性

对于我们其他正在学习长生不老药的人,我们可以这样做:

iex(8)> t String
@type t() :: binary()
@type codepoint() :: t()
@type grapheme() :: t()
@type pattern() :: t() | [t()] | :binary.cp()
iex(9)> t(String)
@type t() :: binary()
@type codepoint() :: t()
@type grapheme() :: t()
@type pattern() :: t() | [t()] | :binary.cp()

也适用于 .t

如果要引用“字符串”类型(由 String 模块中的函数),请改用 String.t/0 类型。

来源https://hexdocs.pm/elixir/typespecs.html#notes

【问题讨论】:

标签: elixir


【解决方案1】:

这不是属性。基本上它是对 Elixir 的 String 模块中的函数使用的“字符串”类型的引用。要完全理解它,请阅读更多here

使用String.t() 的一个原因是Elixir discourages the use of string()string() 是 Elixir 中的“字符列表”,而 String.t() 是 UTF-8 编码的二进制文件。

【讨论】:

  • 这是文档hexdocs.pm/elixir/typespecs.html#notes的正确链接如果要引用“字符串”类型(String模块中的函数操作的类型),请使用String.t/0类型而是。
  • 一个链接指向另一个链接 - 两者都是正确的。这不是你要找的吗?
猜你喜欢
  • 1970-01-01
  • 2019-07-30
  • 2015-12-03
  • 1970-01-01
  • 2016-02-01
  • 2021-09-04
  • 2018-10-30
  • 1970-01-01
  • 2016-08-26
相关资源
最近更新 更多