[erlang-questions] Types and specs [WAS: [enhancement] string:split/2]

Kostis Sagonas kostis@REDACTED
Fri Oct 10 18:57:28 CEST 2008


Edwin Fine wrote:
> Question - trying to understand the spec, if I did the following:
> 
> Eshell V5.6.4  (abort with ^G)
> 1> string:tokens([], "abc").
> []
> 
> Does that violate the type specification, or does it allow for the 
> return of an empty list?

Note that the return type is specified as: [[char(),...]]
i.e., a list of non-empty strings.  The outer list can be empty or not.
It's the strings inside it that cannot be empty.

If the function returned a list of at least one non-empty string, it 
would have the following type instead:

	[[char(),...],...]

If empty strings were allowed but there needed to be at least one of 
them, the return type would have been:

	[[char()],...]

Hope this clears up the issue.

Kostis

> On Fri, Oct 10, 2008 at 7:33 AM, Kostis Sagonas <kostis@REDACTED 
> <mailto:kostis@REDACTED>> wrote:
> 
>     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
>     _______________________________________________
>     erlang-questions mailing list
>     erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>     http://www.erlang.org/mailman/listinfo/erlang-questions
> 
> 




More information about the erlang-questions mailing list