Syntax to augment keyword

I often would like to use | with keywords. Is there a technical / grammar issue that prevents Elixir from writing it using the shortcut notation?

tail = [3, 4]
list = [1, 2 | tail]
# => [1, 2, 3, 4]

tail = [c: 3, d: 4]
list = [a: 1, b: 2 | tail]
# => ** (CompileError) iex:7: misplaced operator |/2

I have to write using the long form, or use ++ which does not look as nice and that many would believe (apparently mistakenly! thanks hauleth and benwilson512) that would be less efficient

list = [{:a, 1}, {b: 2} | tail] # => ok
list = [a: 1, b: 2] ++ tail # => ok, might appear not as efficient

Edit: tweaked my question after being corrected about performance

1 Like

That “not as efficient” is AFAIK lie. There should be no performance difference.

2 Likes

Yup, ++ is exactly has efficient as | when you have literals on the left hand side.

1 Like

Oh wow, good to know thanks.

I edited my question, as I still would like the intuitive (to me) shorthand to work.

It is because [foo: integer | float] is the typespec notation.

It was a mistake to use the same notation for both but one we cannot address now. It is one of the many reasons we are considering using or for unions in the type system prototypes we have been working on.

7 Likes