iamacube

iamacube

Understanding improper lists usage in elixir-plug

:wave:t3:

I want to understand the reason of constructing an improper list usage in the elixir-plug repository, Line 256

First Question: Is the usage of an improper list necessary here?:

# encoder fallback
  defp encode_pair(field, value, encoder) do
    [field, ?= | encode_value(value, encoder)]
  end

I think improper lists do not have many uses; also there are many functions that will error when given an improper list.

for example Dialyzer would raise a warning for constructing an improper list, and they got this -dialyzer({no_improper_lists, g/0}). to suppress the warning.

Second Question: why this construct actually exists in Erlang?

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You might not be wrong about this function, it just isn’t super obvious to me that Dialyzer is right either. Either way, check out this line: plug/lib/plug/conn/query.ex at 274e44f9a149b922099bf60029d8267afe494968 · elixir-plug/plug · GitHub

The encode_pair function is a defp and the output of that function is passed immediately to IO.iodata_to_binary. iodata is one of the real world uses for improper lists, because it reduces the overhead of building out a list front to back. The iodata_to_binary is able to handle improper lists and turn them into a single binary.

Dialyzer warns about improper lists because most of the time you do indeed want to warn for them. If the Plug core team used dialyzer they would probably put a no-warn invocation in there to tell dialyzer that this is intentional.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I don’t think this does create an improper list. It’s a recursive function, and that function has termination clauses that return [], which means it’ll construct a proper list.

As to this:

I think it’s better to approach this by understanding that the cons operator | is, at least in erlang, more foundational than even lists. Its most common use is with lists, but it has uses for low memory btrees, and gets use in IOlists as well.

sabiwara

sabiwara

Elixir Core Team

Sorry for resurrecting an old thread, but while looking into this I also stumbled upon this issue which is relevant I think:

The OTP team seems to be considering disabling these warnings, since there are legit use cases for improper lists:

  • IO data as already explained above
  • as a low-level performance optimization for libraries: [a | b] takes less memory than {a, b}

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement