Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

@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].
like image 820
lapinkoira Avatar asked Jan 29 '23 00:01

lapinkoira


1 Answers

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.

like image 94
Dogbert Avatar answered Jun 06 '23 20:06

Dogbert