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

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
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
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
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New

Other popular topics Top

marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
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 53690 245
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement