bartblast

bartblast

Creator of Hologram

Keyword inside tuple shorthand syntax {a: 1, b: 2}

This could resolve to {[a: 1, b: 2]}. Was it ever considered to allow such syntax? Notice this: {:abc, a: 1, b: 2} and this: my_fun(:abc, a: 1, b: 2) work as I described. As I understand, the compiler is able to figure out that the second part is a keyword list.

I can see that it is similar to %{a: 1, b: 2}, and it may be misleading for people coming from Ruby (hash syntax).

Context: I’m working on Hologram’s template engine. If the shorthand syntax worked for tuples as well, it would simplify a few things since Hologram uses tuples for interpolation and prop syntax, e.g.

<MyComponent my_prop={:my_value} />

or

<button $click={:do_something, a: 1, b: 2}>Click me</button>

or shorthand class attribute syntax:

<div class={button: true, "button-active": false}></div>

This issue can be handled by the template parser, but I feel that it may be beneficial to support it in the Elixir compiler.

Most Liked

al2o3cr

al2o3cr

My supported-only-by-vibes take is that when a user writes {a: 1, b: 2} they are far more likely to be typoing either a map literal (missing %) or a kwlist literal (wrong kind of bracket). :man_shrugging:

LostKobrakai

LostKobrakai

Looking at Syntax reference — Elixir v1.20.2 I’m wondering if the syntax sugar in tuples should rather be removed. The atom shorthand within […] and %{…} works the same, but is considered a feature available for both those types and is not available for tuples.

The syntax sugar for skipping square brackets is documented to apply for the last parameter of calls. Creating a tuple is not a call. The closest comparison to the creation of a tuple would be the creation of a binary <<…>>.

Eiji

Eiji

@gregvaughn Yes, it’s indeed confusing - the syntax, see:

iex> {:do_something, a: 1, b: 2}
{:do_something, [a: 1, b: 2]}
iex> {a: 1, b: 2}
** (SyntaxError) invalid syntax found on iex:2:1:
    error: unexpected keyword list inside tuple. Did you mean to write a map (using %{...}) or a list (using [...]) instead? Syntax error after: '{'
    │
  2 │ {a: 1, b: 2}
    │ ^
    │
    └─ iex:2:1
    (iex 1.17.0-rc.1) lib/iex/evaluator.ex:295: IEx.Evaluator.parse_eval_inspect/4
    (iex 1.17.0-rc.1) lib/iex/evaluator.ex:187: IEx.Evaluator.loop/1
    (iex 1.17.0-rc.1) lib/iex/evaluator.ex:32: IEx.Evaluator.init/5
    (stdlib 6.0) proc_lib.erl:329: :proc_lib.init_p_do_apply/3

How about something like this?
<div {:class, button: true, "button-active": false}></div>

Where Next?

Popular in Proposals: Ideas Top

GenericJam
I’ve been messing around with image generation models recently and thought this could be wrapped in a library or people could just use th...
New
cheerfulstoic
I feel like Elixir is getting big enough and old enough that I’m starting to experience problems with conflicting dependencies. An examp...
New
jakeprem
Goal: To make JS.patch and JS.navigate more interoperable with JS.push. Scenario: Imagine making a reusable Phoenix component and you wa...
New
cevado
IEx is a very powerfull shell and it would be awesome to have all this power integrated inside a code editor. Clojure enables something l...
New
mortenlund
Hi! I would like to suggest a new callback in the lifecycle of the Live Component which is unmount. Sometimes it is nice to be able to ...
New
bartblast
This could resolve to {[a: 1, b: 2]}. Was it ever considered to allow such syntax? Notice this: {:abc, a: 1, b: 2} and this: my_fun(:abc,...
New
pierrelegall
Problem Currently, List.first/2 and List.last/2 return a default value (or nil) when the list is empty. However, there are cases where an...
New
dvartic
Would there be interest to implement a link/1 that gives to option to operate on other events? So I implemented a simple link/1 function...
New
PJUllrich
Hey folks, I have the unique problem that I need to ignore all “change” and “input” events for one specific input element in a LiveView F...
New
sodapopcan
This past weekend I’ve been doing some work on a PR to bring open_browser to PhoenixTest. For those who don’t know it, PhoenixTest is a ...
New

Other popular topics Top

New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement