简体   繁体   English

typespec中的二进制,String.t,char_list和[char]之间的区别

[英]Difference between binary, String.t, char_list and [char] in a typespec

Which are the different between binary, String.t, char_list, [char] etc when defining a typespec? 定义typespec时,binary,String.t,char_list,[char]等之间有何区别?

@spec method(binary) :: binary

and

@spec method(String.t) :: String.t

Are they equivalent? 它们相等吗? Doc says: Doc说:

  • binary Used for Elixir strings. 二进制用于Elixir字符串。
  • char_list Used for Erlang strings. char_list用于Erlang字符串。 Defined as [char]. 定义为[char]。

String.t is the same as binary , see its definition here . String.tbinary相同,请参见此处的定义。

char_list is more than a plain list of chars. char_list不仅仅是一个普通的char列表。 It's defined in the unicode module in Erlang to be: 在Erlang的unicode模块中定义为:

maybe_improper_list(char() | unicode_binary() | charlist(),
                    unicode_binary() | [])

So it includes both proper and improper lists and the list can contain chars, binaries, or charlists or the empty list. 因此,它既包含正确的列表,也包含不正确的列表,并且该列表可以包含字符,二进制文件或字符列表或空列表。

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

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