iamacube

iamacube

Understanding improper lists usage in elixir-plug

:waving_hand: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?:

https://github.com/elixir-plug/plug/blob/274e44f9a149b922099bf60029d8267afe494968/lib/plug/conn/query.ex#L256

# 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:
https://github.com/erlang/otp/issues/5937

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

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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 31494 112
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
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement