[erlang-questions] Types and specs [WAS: [enhancement] string:split/2]
Tue Oct 14 08:41:47 CEST 2008
Richard O'Keefe wrote:
> On 13 Oct 2008, at 7:49 pm, Kostis Sagonas wrote:
>> Richard O'Keefe wrote:
>>> On 11 Oct 2008, at 12:33 am, Kostis Sagonas wrote that
>>> [[char(),...]] means list of non-empty strings, which
>>> is more precise than [string()].
>>> Would [char()|string()] also work?
>> No. This is a different type, which does not express what you want here.
>> Note that in the language of types | does not mean list tail, but
>> instead means type union. The above type denotes a (possibly empty)
>> list consisting of characters or strings.
> Then we have a rather nasty syntax problem here.
> If I had written [(char() | string()] where the bar is NOT
> part of the list syntax, I would certainly expect that to
> mean a list of things each of which is a char or a string.
> But with the vertical bar being part of the list syntax,
> I would expect [H | T] as a type to mean the type of cons
> cells whose heads are of type H and whose tails are of type T.
The language of types does not have any convenient notation for lists
with a tail other than . The [...] notation is a shorthand for list()
and *always* refers to proper lists, so there's no ambiguity there.
(If you want to specify an improper list the closer you can get is to
employ the long and yucky type maybe_improper_list() that does not have
any convenient shorthand. As its name suggests, you cannot even be
specify with certainty that the list is improper.)
> If it *doesn't* mean that, it should be a syntax error.
I agree with you that the version with parentheses looks and feels
better. You are welcome to submit a patch for this.
More information about the erlang-questions