przemyxe0p

przemyxe0p

Understanding types definition, iolist

Hello
I am trying to understand types definitions in documentation https://hexdocs.pm/elixir/typespecs.html#built-in-types

Especially iolist() which is defined as

maybe_improper_list(byte() | binary() | iolist(), binary() | [])

I am not sure how to read pipe (|) and coma (,) in type definitions.

ChatGPT returned following:

:two: First Part: byte() | binary() | iolist()

This means each element in the list can be:

  • byte(): An integer from 0..255 (a single byte).
  • binary(): A binary string (<<...>>).
  • iolist(): Another iolist() (allowing nested lists).

:three: Second Part: binary() | []

This means the tail of the list can be either:

  • binary(): The list may end with a binary (improper list).
  • []: The list may end properly (normal list).

Which I’m not sure is true.
Please clarify

Most Liked

fuelen

fuelen

This is true.
Improper list is a list where the tail is not an empty list.

# proper list:
iex(1)> [1, 2 | []]
[1, 2]

# improper list
iex(2)> [1, 2 | 3]
[1, 2 | 3]

| pipe means or. Think about maybe_improper_list as about function that returns a type, so it has 2 args and that’s why , comma is used.

garazdawi

garazdawi

Erlang Core Team

The second part describes the tail end of the entire list, not the tail of any list item. For example:

[1, 2, 3 | [] ]

In the above, the “First Part” in the type describes 1, 2, 3 and the “Second Part” describes only []. So in your example [h | t ], the type of t is the union of the “first part” and “second part” unless we know that it is the end of the list, then it is only the “second part”.

gregvaughn

gregvaughn

The binary “constructor” syntax, <<...>> accepts bytes, but it doesn’t produce a byte(). Try for yourself, see if <<0>> equals 0.

Where Next?

Popular in Questions Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement