【问题标题】:Is there a way to use @spec (Function specs) to specify the function return as list of UUIDs?有没有办法使用@spec(函数规范)将函数返回指定为 UUID 列表?
【发布时间】:2021-07-28 02:30:39
【问题描述】:

我有一个名为“get”的函数,它返回一个 UUID 列表。此列表可以为空,也可以有 N 个 UUID。有没有办法使用@spec 来公开这种类型的返回?到目前为止,我的 @specs 告诉我们一个通用列表正在返回。

@spec get :: list
  def get do
    ...
    list_of_UUIDs
  end

【问题讨论】:

    标签: function elixir specifications


    【解决方案1】:

    Ecto.UUID.t() 定义为

    @type t :: <<_::288>>
    

    我不会重新发明轮子,而是会使用

    @spec get :: [<<_::288>>]
    

    它并不完全正确(不是每个36 字节都是有效的UUID),但比list() 要好得多。

    为清楚起见,可能会明确使用

    @type uuid :: <<_::288>>
    
    @spec get :: [uuid()]
    

    【讨论】:

    • &lt;&lt;_::128&gt;&gt; 如果 UUID 是二进制 - 288 是字符串表示所需的位数,包括以 UTF-8 编码的连字符。
    猜你喜欢
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多