pejrich
Why does MapSet use empty lists instead of `nil` for the values of the maps keys?
I was playing around with MapSet, and I turned it into a plain map with Map.from_struct(). I was aware that underneath it was essentially just using map keys to ensure that it was a set, but I wasn’t sure why instead of all the values being nil, they are all empty lists ([]). Is there a reason why?
iex(16)> MapSet.new() |> MapSet.put(:a) |> MapSet.put(:b) |> Map.from_struct
%{map: %{a: [], b: []}, version: 2}
Marked As Solved
lpil
Looks like it was done to reduce space used when serialised using Erlang term format
Also Liked
NobbZ
The empty list is basically the NULL pointer internally. That’s why it is that small. It can be represented in a single byte which is usually used to tag the type of the following data.
hauleth
In BEAM there is no nil (not true and false), these are just regular atoms nil == :nil. That is why the empty list, which is “special value” has the smallest representation (in ETF nil need to encode its name as well as it can not exist on target machine or can be in different place, while the empty list will always be there and will always occupy the same “space” in all instances).
OvermindDL1
Well technically in the BEAM the empty list of [] is called the nil type internally, that’s traditional among many languages, the empty list is nil, I don’t know why Elixir uses an atom for that, it’s still really really weird to me even to this day… I still assert that nil in Elixir should have originally mapped to [] on the BEAM as one would expect a nil type to do. ^.^;
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









