amnu3387
How to use strings as keys on Maps?
Hi,
so when I Map.put even if I use the string quotation marks on the key Elixir will render the map with atoms. This is further problematic when I save data into the db as JSON, because when retrieving this data, it is all as string keys and no atoms.
Amap
|> Map.put("player_#{number}", %{
roll: false,
id: player.id,
stops_self: %{
"aether_charge": true
}})
The real use case is that, when I first create this map, it’s through an individual process, that ultimately, saves it to the db, so if the process gets killed or crashes, when being served again, the db version will be fetched.
Is there any way I can enforce a map to use string keys from the get go? So as to always have to deal with a single type of keys. I’m aware I could write a module that returns either atom or string keys but is there any easy way to be explicit about what type I want my key to be?
Thanks
Marked As Solved
NobbZ
You are not using a string, but quoted atom. Those are meant to be used, when you have symbols which usually can’t be in a simple atom literal: :"$end_of_table". If you want to use a string as key in a map do it like this: %{"key" => 1}.
Also Liked
amnu3387
amnu3387
LOL then this is much better definitively ^^
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










