silverdr
Map.get vs. Access - pros/cons?
The documentation says:
Keys in maps can be accessed through some of the functions in this module (such as
Map.get/3orMap.fetch/2) or through themap[]syntax provided by theAccessmodule
Are there any pros/cons for using one over another? I mean especially in situations where the result would be the same. Also any performance / resource usage implications of using one over another?
Most Liked
sergio
fetch/2will give you an error and say HEY THIS DOES NOT EXIST. Do something about it!
Map.fetch(medical_kit, :insulin_shot)
:error # CALL 911!
Map.get/2 is more chill and will be like hey uh I couldn’t find anything here’s nil - or give you a default. Like asking your wife for your Tom Ford sunglasses and honey if you can’t find it just grab me my Raybans no big deal.
Map.get(dresser, :tom_ford_sunglasses, :raybans)
marcandre
Except map_or_kw[:something] || ”bananas" will behave differently on %{something: false} for example.
BartOtten
Additionally, the Access syntax is also supported by Keyword List; so it allows for switching between Map en Keyword List without replacing calls.
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
- #javascript
- #code-sync
- #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









