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:
First Part: byte() | binary() | iolist()
This means each element in the list can be:
byte(): An integer from0..255(a single byte).binary(): A binary string (<<...>>).iolist(): Anotheriolist()(allowing nested lists).
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
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
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
The binary “constructor” syntax, <<...>> accepts bytes, but it doesn’t produce a byte(). Try for yourself, see if <<0>> equals 0.
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








