> Erlang already is a more concise language than Elixir but also noisier as there are more punctuation characters.
What?
Here's more-or-less the entirety of Erlang's "noisier syntax with more punctuation characters"
-spec f(A :: any(), B :: some_type(), C :: list()) -> any().
f(A, {B}, [C | _]) ->
A1 = fun() -> io:format(a, []) end,
A1(),
case C of
<<1, _/binary>> -> 1;
_ -> #person{ok = ok, field = C}
end.
Edit:
%% plus map syntax
#{"tuple" => {1,2}}
M#{"key" := "new_value"}
%% plus list comprehensions
[X || X <- List, X /= 10].
Elixir has all that plus more. The equivalent in Elixir is something along the lines of
@spec f(a :: any(), b :: some_type(), c :: list()) :: any()
def f(a, {b}, [c | _]) ->
a1 = fun() -> IO.inspect(a) end,
a1.(),
case C do
<<1, _ :: binary>> -> 1
_ -> %Person{ok: :ok, field: c}
end
end
## and don't forget the capture and pipe syntax
x |> (&f(y, {&1}, list)).()
## and default function parameters
def f(a, b, c \\ []), do: something()
## and sigils
String.replace(str, ~r".*", "")
## and Ecto adds its own characters
id = 1
(from p in Person, where: p.id == ^id, select: p) |> Repo.all()
## and dropping down to Erlang is a function call on an atom
:io.format(x, y)
And I'm definitely forgetting more...
Edit:
## and string interpolation
a = 1
s = "The value is: #{a}"
## and two different map syntaxes
%{a: :map, with: "various", keys: "as", atoms: 0}
%{"another" => "map", "but" => "keys", "are" => "strings"}
## and different map access
map[key]
map.key
## and map update
%{ map | key: "new_value" }
## and list comprehensions
for x <- list, x != 10 do
end
Your Elixir example is literally wrong. It won’t compile unless you do many changes. You are keeping -> instead do. You are still using commas after each expression. The fn syntax is wrong and also using parens for args. So you forgot to remove most of the punctuation noise.
You are conveniently not showing many examples where Elixir is considerably less noisier such as keywords, utf-8 binary strings, etc. Module definitions in Erlang use -(). while Elixir doesn’t use a single punctuation character. Erlang has special syntax for list and binary comprehensions while Elixir has unified both and uses no special syntax. Maps in Erlang also have two syntaxes, more ways of doing try/catch, etc.
If you take examples of actual code implemented in both languages, instead of cherry-picked invalid gibberish, you will find that Erlang has more punctuation per character than Elixir.
I mean, Erlang doesn't even have `&` as a part of syntax, but sure, "Erlang has more punctuation".
Besides, Erlang's syntax is significantly more consistent than Elixir's. Just take Elixir's two separate syntaxes for maps, for example. Or the dichotomy between syntax for functions and lambdas (both for declaration and invocation).
The only reason Elixir feels like it has less punctuation is that Erlang is actually more terse than Elixir.
Just having pipes and function captures everywhere makes Elixir code have more punctuation than Erlang.
(And no, using Erlang strings here is not the same, especially when popular libraries like Hackney and Cowboy work only with binary strings)
> Erlang is actually more terse than Elixir
Which was my point since the beginning but you quoted only part of my reply. It is clear Elixir has more syntax than Erlang. The function definition delimiters (->/.) vs (do/end) is a good example in itself of how Erlang is more compact and using punctuation.
I stand with my position that if you take actual code, a file or a project, and write it in both idiomatic Erlang and Elixir, Erlang will have more punctuation per character and will feel noisier.
Here is an actual example. I got the hex decoding/encoding code recently committed to OTP in Erlang (https://pastebin.com/qMtj8mSY) and rewrote to Elixir (https://pastebin.com/zUCLeXZG). I ran them through the Whatsapp formatter and the Elixir formatter respectively. Both snippets compile and define proper modules. If you remove all whitespace, the rate of punctuation character per alphanumeric character for Erlang is 35%. For Elixir it is 25%. And FWIW, in this particular case, the Elixir implementation has roughly the same amount of characters as the Erlang one: 718 vs 721.
I will be glad to continue this discussion if you want to use actual code instead of fictional examples.
EDIT: I fixed an error in the Erlang snippet and updated the stats.
I accept that my original sentence was unclear. I mentioned more punctuation characters in the context of conciseness, which obviously takes the amount of characters into account. Other than that, I don’t dispute Erlang has less syntax and I hope it is clear that Erlang is noisier (assuming the definition of noise is punctuation / character).
Regarding the compiler, most compilers have ambiguity. It is the reason why you have to put a space between = and binaries in Erlang. The question is if the compiler is going to pick a side or require the developer to be explicit. Modern compilers prefer to fail early in such cases, rather than letting a syntax error pop up later on.
Furthermore, the use of do/end vs do: is completely up to you. My original draft had only the first, which reduced the amount of punctuation in Elixir further, but I decided to include both styles because you will find both in practice. But if you want to stay consistent, you have the option.
Finally, happy to disagree on the “agglomeration of characters” in the Elixir example. The Elixir code has less punctuation and is clearer, despite the use of “do:” (which, as I said above, is optional).
> Erlang already is a more concise language than Elixir but also noisier as there are more punctuation characters.
Nope. Elixir has more punctuation characters, and I've shown that.
Now, when we talk about "punctuation per characters of code", then yes, Erlang may have more punctuation in this regard. But it has another thing going for it: there's significantly less syntax in general, and it needs less brainpower to disambiguate (BTW, Elixir's syntax is ambiguous to the point that the compiler can't figure it out in certain contexts).
Even in the examples you provided:
defp encode_hex(<<a::4, b::4, rest::binary>>, acc) do
a = encode_hex_digit(a)
b = encode_hex_digit(b)
encode_hex(rest, <<acc::binary, a, b>>)
end
defp encode_hex_digit(char) when char <= 9, do: char + ?0
defp encode_hex_digit(char) when char <= 15, do: char + ?a - 10
Oh, look, it was `f() do ... end` but then all of a sudden `f(), do: ` (with no end). Whereas Erlang's syntax is (mostly) the same forms everywhere.
And where Erlang is consistent due to terseness of syntax:
defp decode_hex_char(char) when char in ?a..?f, do: char - ?a + 10
I have complained about Elixir syntax in the past [1] when I knew little to no Elixir (I'm now building a big-ish side project in it), but the complaint mostly remains.
Meanwhile Erlang may look like it uses more punctuation, but that punctuation can be much easier pattern-matched by our brains because it's consistent and means the same in (almost) all cases.
That’s not a representative example of none of the languages. It is also just plain invalid for Elixir. I recommend checking actual code examples on their websites to form a better opinion.
It's not that bad, really. As with most languages, syntax is the easy part.
From personal experience:
- You're comfortable with Erlang syntax within half a day. There's just so much less of it
- You're comfortable with Elixir syntax within a day of two of active coding. Function captures may trip you up once or twice more, but then it's fine.
I know that "" in Elixir is binaries which are <<>> in Erlang. In Erlang "hello world" is really a list of ["h", "e", "l", "l", ...].
What was surprising to me is how rarely that matters (unless you actually do a lot of text processing). And the functions that matter (like I/O) deal with io_lists anyway.
And since strings in Elixir are binaries, when you actually need to work with binaries, you're back to the same/similar syntax:
What?
Here's more-or-less the entirety of Erlang's "noisier syntax with more punctuation characters"
Edit: Elixir has all that plus more. The equivalent in Elixir is something along the lines of And I'm definitely forgetting more...Edit: