JEG2

JEG2

Author of Designing Elixir Systems with OTP

Do I have these string definitions right?

Elixir’s terminology related string data gets a little twisty. Do I properly understand these terms?

  • binary: A <<…>> construct with a size divisible by 8
  • bitstring: A <<…>> construct with a size not divisible by 8
  • charlist: A proper or improper list of numeric codepoints, binaries, and/or nested charlists
  • chardata: A charlist or a binary
  • iolist: A proper or improper list of numeric bytes, binaries, and/or nested iolists
  • iodata: An iolist or a binary

I think I’m the least sure about the term charlist. It seems to be defined two ways in Erlang: as a list of codepoints or the definition I used above. If you can share any insight into this duality, you are my new best friend!

Most Liked

josevalim

josevalim

Creator of Elixir

Great answer!

I would just like to add that this is basically why we chose strings to be represented as binaries instead of lists. If you see a list of integers, you don’t know if the integers are meant to represent bytes (iolist/iodata) or unicode codepoints (charlist/chardata). With binaries, because you have the underlying representation, you don’t have this dichotomy: it is what it is.

sinetris

sinetris

Hi James, mostly correct :slight_smile:

Tl;dr all correct but:

  • bitstring can be divisible by 8
  • charlist in Erlang is like iolist but for unicode; in Elixir is a list of numeric representation of unicode chars.

Extended version

A bitstring is a sequence of zero or more bits, where the number of bits does not need to be divisible by 8. If the number of bits is divisible by 8, the bitstring is also a binary. (From Erlang bit syntax doc.)

In Erlang a list of codepoints is called a string. The confusion originate from the fact that Erlang strings are similar to char list in Elixir.

'olé' # [111, 108, 233]

and Elixir strings are binary encoded in UTF8.

<<"olé">> === "olé" # true

The Erlang charlist you linked is the unicode:charlist()
An explanetion for charlist with an example:

[8364, <<" is the euro sign">>] |> to_string # "€ is the euro sign"

Erlang iolist is almost the same as Erlang charlist but with the limitation to 8bit. So the previous example is not a valid iolist but this one is:

[111, 108, 233, <<"olé">>] |> to_string # "oléolé"

I hope this clarify something, or at least don’t create more confusion :smile:

Duilio

josevalim

josevalim

Creator of Elixir

@nathanl that’s a very good question.

You are correct. When you are processing a binary, you need to know what is the encoding of the data in the binary. If I have functions that expect a UTF-8 encoded binary and I pass a UTF-16 binary, the results would be incorrect. That’s similar to a list. If I have a list, I need to know what the integers represent.

My comment was exclusive to sending/writing the data. Because the data in a binary is already encoded, functions that write to files, sockets, io, etc do not care about the encoding of the binary because the data is already encoded. That’s a contrast to lists where I need to know what the integers in them represent to properly send/write the data.

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127536 1222
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New

We're in Beta

About us Mission Statement