Shorthand for passing variables by name?

Unless I missed something, there is shorthand for passing properties when you have a variable of the same name?

I find myself having to write a lot of boilerplate code like this:

    {:ok, assign(socket, workspace_id: workspace_id, widget: widget, thing_name: thing_name)}

Having to write (and read) workspace_id: workspace_id and so on for each variable is a little verbose.

JavaScript added shorthand property names in ES2015

Basically, it makes:

let o = {a, b, c}

syntactic sugar for

let o = {a: a, b: b, c: c}

It would be really neat if we had something similar in Elixir.

Maybe something like

    {:ok, assign(socket, %[workspace_id, widget, thing_name])}

Using %[ to distinguish it from a list - ie. keyword list shorthand.

There are packages on hex which give you what you are asking for, though they don’t allow nesting easily.

As far as I know, they all are not used by their authors anymore and considered “not idiomatic to use” by them.

One of them is named shorter_maps or something like that.

There was this proposal…

3 Likes