Search code examples
typeserlangtype-systemsbeam

Erlang's typer deduced weird types for strings


I'm exploring typer, and gave it a function that does nothing but:

const_str() -> "qwe".

That guy's type is deduced as:

-spec const_str() -> [101 | 113 | 119,...]

, i.e. "eqw" (huh?!), followed by this '...' business.

It looks like constant strings are confusing typer; I understand I shouldn't be using them like this, there are atoms for that purpose; but as I am trying to wrap my head around typer (and Erlang's type options), I thought this is surprising and interesting. Could anyone explain what's happening here? Thanks!


Solution

    1. Strings in Erlang are lists of integers that correspond to the ASCII code of the characters (i.e. "qwe" = [$q,$w,$e] = [113,119,101]).
    2. The type language cannot express the order of a list's elements (and does not aim to do so).
    3. The type you got is that of "a nonempty list containing numbers 101, 113 and 119", which is as close as the inference can get.