[erlang-questions] Types and specs [WAS: [enhancement] string:split/2]
Kostis Sagonas
kostis@REDACTED
Fri Oct 10 13:33:12 CEST 2008
Richard O'Keefe wrote:
>
> Can anyone explain to me why
>
> -spec(tokens/2 :: (string(), string()) -> [[char(),...]]).
>
> doesn't mention "string()" on the right hand side of the arrow?
There is no native string() type in Erlang, as we all well know.
So, in the language ot types, string() is just an alias for the type
list(char()), which, for convenience, is typically written as [char()].
This can include both the empty string "" == [] and a non-empty list
of characters, which is denoted [char(),...]. That's what the above
spec signifies: the resulting list consists of non-empty strings, which
is consistent with what string:tokens/2 actually returns. Hope this
explains things.
On the other hand, I can see how this may look a bit strange to some and
have for a long time now pondered whether a non_empty_string() type
alias might be added to the list of built-in types.
Kostis
More information about the erlang-questions
mailing list