anders

anders

Is there any discussion about adding something similar to ES6 “property value shorthands” to maps? I’m just getting started with elixir so I’m not sure where to look for this type of language discussion.

What do you think about the idea? Would it be possible?

In Java Script (ES6) one can create an object like this:

let x = 50;
let y = 100;

let coordinates = { x, y };

which is shorthand for:

var coordinates = { x: x, y: y }

It would be great to have some thing similar in elixir:

iex> fu = "bar"
"bar"

iex> %{fu}  # Shorthand for %{fu: fu}
%{fu: "bar"}

and it could be used to bind variables in matches:

fn(%{x, y})  # Shorthand for fn(%{x: x, y: y})

Showing Posts 1 to 6

OvermindDL1

OvermindDL1

Library is already made that does all that (in both atom and string key variants): short_maps | Hex ^.^

EDIT: Sadly it is an all or nothing right now, no mixing short and long form, which really sucks in some cases, but it is a feature that ‘could’ be added to it if someone were to PR it perhaps? Would also be cool to make the atom/string choosing direct so :something maps to an atom and something does a string key match instead of the trailing modifier.

sotojuan

sotojuan

I was surprised to learn Elixir doesn’t support this as it seems similar to pattern matching. @josevalim why is this?

anders

anders OP

Thaks for pointing me to the library @OvermindDL1!

Also found a link to a discussion on the elixir mailing list on the short_maps librarys github page.

OvermindDL1

OvermindDL1

There was actually a huge conversation about this with jose a while back at: Redirecting to Google Groups

EDIT: @anders beat me to the link. ^.^

OvermindDL1

OvermindDL1

On an aside, I’d love this syntax or:

iex> %{:a, "b", :c => d, 'e', f: %{:g}} = {a: 1, "b" => 2, c: 3, 'e' => 4, f: %{g: 5}}
iex> a
1
iex> b
2
iex> c
** (CompileError) iex:25: undefined function c/0
iex> d
3
iex> e
4
iex> g
5

It would be able to handle all the basic types for anything from phoenix param matching to erlang strings to atoms to going back to normal pattern matching. Even if this were as a sigil maybe like m%{:a, "b", :c => d, 'e', f: m%{:g}} or something. Could have a base case of an undecorated variable fall back to being just an atom (the default case), and support adding ^ to any of them to match an equal named variable.

anders

anders OP

Perhaps it’s because I’m new to elixir (and not used to seeing sigils), but the “undecorated” style is much more appealing to me. This feature is all about reducing character noise and repetition and, to me, the sigils introduce another kind of noise that makes the code less fluent to read.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews