[erlang-questions] String type

Kostis Sagonas kostis@REDACTED
Sat Jun 23 14:07:02 CEST 2018


On 06/22/2018 11:41 PM, Sam Overdorf wrote:
> Has anyone considered making string a type and not a list of chars.
> 
> I seem to have a lot of trouble when a list is a bunch of string
> objects and I start taking it apart with [H|T] = List..
> 
>   When processing the last string in the list I end up taking apart the
> individual characters of the string. If I do a type-check it tells me
> it is a list.
> 
> I usually have to do a work around to handle this. If it was a type I
> would easily know when I am done with the list.

The various pros and cons of having a special string type in Erlang have 
been discussed before in this mailing list; please refer to the archives 
of the list for finding them.

The arguments for a string data type are often more involved than yours 
and typically are centered around space concerns.  If your only grief 
with strings-as-lists is the above "programming confusion", may I 
suggest that you wrap your strings inside a tuple-pair with a 'str' tag?

I.e., instead of a list of strings ["hello", "world"] you process lists 
of the form [{str,"hello"}, {str,"world"}].

When you become more comfortable with list processing in general, you 
can then drop the 'str' tuple wrappers from your code, and most probably 
you will also realize that the list(string()) representation is not so 
bad after all.

Kostis



More information about the erlang-questions mailing list