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

2.3k views Asked by At

Which are the different between binary, String.t, char_list, [char] etc when defining a typespec?

@spec method(binary) :: binary

and

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

Are they equivalent? Doc says:

  • binary Used for Elixir strings.
  • char_list Used for Erlang strings. Defined as [char].
1

There are 1 answers

5
Dogbert On BEST ANSWER

String.t is the same as binary, see its definition here.

char_list is more than a plain list of chars. It's defined in the unicode module in Erlang to be:

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.