JEG2
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 8bitstring: A<<…>>construct with a size not divisible by 8charlist: A proper or improper list of numeric codepoints, binaries, and/or nested charlistschardata: A charlist or a binaryiolist: A proper or improper list of numeric bytes, binaries, and/or nested iolistsiodata: 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
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
Hi James, mostly correct ![]()
Tl;dr all correct but:
bitstringcan be divisible by 8charlistin Erlang is likeiolistbut 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 ![]()
Duilio
josevalim
@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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex










