cevado
Should we have namespaced atoms in Elixir?
My idea comes from qualified keywords from Clojure, at least after I got in contact with that in Clojure I felt Elixir was missing something like that. So just a small explanation of what is a qualified keyword in Clojure that I got from this blog post:
Brief: the five common forms
:foo, which is just your plain old keyword::foo, which is a namespaced keyword for the current namespace:my.ns/name, which is a namespaced keyword for a valid namespace::my/name, which uses the:asalias to achieve the same as form #3:something/foo, which is commonly used with Datomic and doesn’t actually map to a valid namespace
I think 4 can be ignored for now but to understand 2 and 4:
(ns some.namespace)
(def foo {::bar 1})
outside of the some.namespace ::bar gonna be :some.namespace/bar.
You still can have 5, that is :something/foo that doesn’t actually relate to any existing namespace.
The idea is that we can have some context aware keyword/atom. I’m not suggesting binding atoms to module namespaces or something like that, but I guess we can initially do something like 5 at least for now.
Now going effectively to Elixir stuff.
We can have @ inside of an atom that doesn’t break the atom syntax, either in Elixir or Erlang. We could use that to standardize in the community an approach for namespaced atoms, something like :foo@something. The idea is that we can read that as “foo at something”.
Why use that instead of just old snake case? Exactly because that a simple snake case doesn’t communicate any intetion of namespacing or context related stuff to an atom.
Just as an example, let’s say want I to store api tokens for facebook and twitter I could use %{twitter_api_token: twitter_token, facebook_api_token: fb_token} I could have something like %{api_token@twitter: twitter_token, api_token@facebook: fb_token}. I think this even help avoid unneccesarry nesting of structures.
Most Liked
dimitarvp
At that point you might be much better off served by tagged tuples – potentially on several levels, not just one, e.g. {:api_token, :twitter, "abcdef} – than doing dances with your own naming convention of module-like atom names. Because they’re just that: conventions. You have make all your code abide to them, whereas with complex tuples you can utilize pattern matching and eliminate bugs by the mere virtue of missing function head definitions [for invalid state].
Added bonus is that there’s already a fantastic library for that: Domo.
LostKobrakai
Elixir modules are namespaced atoms. They‘re :"Elixir.Module.Name“ aka Module.Name in short. Though the syntax sugar included in elixir doesn‘t really make that reusable.
ityonemo
I think you can alias arbitrary “module-style” atoms, they don’t have to actually be modules, which gets you some of the way there.
Popular in Discussions
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









