cevado

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

  1. :foo, which is just your plain old keyword
  2. ::foo, which is a namespaced keyword for the current namespace
  3. :my.ns/name, which is a namespaced keyword for a valid namespace
  4. ::my/name, which uses the :as alias to achieve the same as form #3
  5. :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

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

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

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.

Where Next?

Popular in Discussions Top

jswny
I would like to better understand what the advantages/disadvantages of umbrella applications are compared to structuring your app as as s...
New
vans163
So useless benchmarks aside, Its possible to write a webserver that can serve 300k requests per second (perhaps more with optimizations)....
New
blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
jeramyRR
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
sashaafm
Piggy backing a bit on @dvcrn topic BEAM optimization for functions with static return type?, I’ve been trying to understand in a deeper ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement