3
votes

Erlang: what is the difference between [string()] and list() ??

I saw them as return types of ct_telnet:cmd and ct_ssh:exec ?

http://erlang.org/doc/man/ct_ssh.html

exec(SSH, Command, Timeout) -> {ok, Data} | {error, Reason}
Types:
  Data = list()

http://erlang.org/doc/man/ct_telnet.html

cmd(Connection, Cmd, Opts) -> {ok, Data} | {error, Reason}
Types:
  Data = [string()]
2

2 Answers

9
votes

The type list() stands for any list, without specifying the type of its elements. Another way to write it is [_] or [term()].

A string() is a special case of list(): it is a list containing integers representing Unicode code points (or Latin-1 characters, if less than 256, or ASCII characters, if less than 128). Another way to write string() is list(char()) or [char()].

A [string()] is a list of strings. This type can also be written as list(string()). Since it is a special case of a list, it is also valid (though less informative) to write it as list().

0
votes

Formally there is no such type as "string" in Erlang, however strings are denoted using a list of codes. So essentially

String() -> [Positive_Integer()] (list of positive integers)

[String()] -> [[Positive_Integer()]] (list of list of positive integers)

where [] denotes a list.