Friday, November 2, 2018

Elixir strings in single quote vs double quote

Today I stumbled on the subtle difference in Elixir between strings in single quote and double quote. Although the documentation explains it clearly, but who reads the documentation these days anyway.

Here is the deal:

  • Single quoted string, is actually a charlist, aka a list of chars. If you want to concatenate them, you need to use list concatenation operator '++'. 
  • 'hello' ++ ' ' ++ 'world' 
  • Double quoted string, is a UTF-8 encoded 'binary', aka a series of bytes. Their concatenation is done via '<>'. 
  • "hello" <> " " <> "world"
  • You can't mix the two
  • "hello" ++ " " ++ "world" # this is wrong
    'hello' ++ " " ++ 'world' # also wrong
    "hello" <> " " ++ 'world' # you just don't get it
  • There are functions to convert
  • to_charlist("hello")
    to_string('hello')

    to_string('hello') <> " world"
    to_charlist("hello") ++ ' world'
  • You may tend to care less about the difference, and just stick to just one type, say always use the double quoted strings. However be aware that some erlang library you end up using unknowingly may use different type and give you cryptic error. For example
    iex> "<h1><a>title</a></h1>" |> SweetXml.xpath(~x"//h1/a/text()")

    'title' 

    Note the return is a charlist even the original input is a string/binary!

No comments:

Post a Comment