Search code examples
erlangdialyzer

Does type [string()] cover empty list?


When defining a type which will be a list of strings but can also be an empty list, do I have to define both cases like this:

-type my_type() :: [string()] | [].

or is this enough:

-type my_type() :: [string()].

Solution

  • Yes, [string()] includes the empty list so using -type my_type() :: [string()]. is fine if you want to include empty lists.

    Because lists are commonly used, they have shorthand type notations. The types list(T) and nonempty_list(T) have the shorthands [T] and [T,...], respectively. The only difference between the two shorthands is that [T] can be an empty list but [T,...] cannot.

    Source