5 ms·
Entirely agreed. Elixir's Macro's are awesome, and Erlang could definitely use an Elixir-Macro-Like Parse Transform library (could definitely exist), Erlang's
by OvermindDL1 8y ago
Entirely agreed. Elixir's Macro's are awesome, and Erlang could definitely use an Elixir-Macro-Like Parse Transform library (could definitely exist), Erlang's syntax I find far more readable, consistent, and logical as well. Elixirs syntax has a lot needless, hard-to-read, noise, but it's not hard to overcome the noise to get the benefits of the overall ecosystem and macros.
- pera 8y agoThis topic is quite subjective, but could you provide some example? Overall Elixir seems to me a bit easier to read, for instance: Elixir: "Hello,How,Are,You,Today" |> String.split(",") |> Enum.join(".") |> IO.puts Erlang: -module(tok). -export([start/0]). start() -> Lst = string:tokens("Hello,How,Are,You,Today",","), io:fwrite("~s~n", [string:join(Lst,".")]), ok.
- dxhdr 8y ago> This topic is quite subjective, but could you provide some example? Elixir's pin operator is a good example of unnecessary complexity.
- dopamean 8y agoWhy is it unnecessary?
- OvermindDL1 8y agoIt's unnecessary in the 'erlang' ecosystem because erlang doesn't have variable rebinding, Elixir 'does' have variable rebinding so it needs some way to distinguish between rebinding and matching, and it was decided to use `^` to mark matching, when honestly I think it would have been smarter to specify it (or something similar) as rebinding. In quite a few cases rebinding leads to bugs (accidentally rebinding something that you still need an old value of).
- mmartinson 8y agoI think I would also prefer there to be no reminding. On the topic of the pin operator though, it seems more clear to me that it's an assertive component of a match, rather than needing to reference all variables in a scope in the case where there was no rebinding, to determine if something matching has already been assigned.
- dnautics 8y agoA buddy of mine and I were talking at length about this and we came to the consensus that the smartest choice would have been: no rebinding by default, if you want rebinding, make it available, say, using "var", or a sigil
- OvermindDL1 8y agoThis is precisely how I think as well, some declaration like `let` or `var` or so to say "This is a new binding"
- dqv 8y agoI'm okay with this trade off instead of having to do Variable0, Variable1, ..., VariableN. It would be cool to have an option to turn off rebinding for those who don't want it (and then not requiring the pin).
- mononcqc 8y agoElixir already has those rebindable variable, which can be "turned off" by using the ^ prefix on a match variable. Wouldn't that make the |> less useful to you already?
- OvermindDL1 8y ago`|>` would replace the great majority of need for multi-staged named variables. In addition, naming variables like that in Erlang is a mis-design, the names should be more descriptive.
- mercer 8y agoI vaguely recall that this particular choice is one that the language's creator also regrets.
- pg_bot 8y agoEven better: "Hello,How,Are,You,Today" |> String.replace(",", ".") |> IO.puts()
- OvermindDL1 8y agoIt still doesn't do the same thing as the erlang code as the elixir example is being run at the staged compile-time instead of run-time like elixir's would be though. And a superfluous `ok` atom is being returned from the erlang code when its `io:fwrite/2` already returns it, plus why call fwrite instead of just write out an iolist directly?! o.O
- mmartinson 8y agoYa I get the sense the Erlang example is written a bad faith a little.
- pera 8y agoNot my intention at all, please feel free to rewrite it: I don't know much Erlang and I actually copied that code from Rosetta Code.
- crad 8y agoApples to apples? I don't do Elixir but this is what I think it's doing: io:fwrite(string:join(string:tokens("Hello,How,Are,You,Today",","),".")).
- OvermindDL1 8y agoEntirely subjective yes! ^.^ However what you show is not focusing on syntax differences but rather function differences, even `|>` is an (macro) operator. By syntax I'm talking about things like the `do`/`end` and `fn`/`end` and `,do:`/`do...end` mismatches, things like atom key short-form of `someatom: ...` (being short for `:someatom => ...`) only being useful at the end of it's list/map context instead of everywhere (which is not ambiguous nor have any other reason not to do it that I can see), having functions be callable with or without parenthesis (of which thankfully the formatter default puts parenthesis) when functions really should be defined with or without parenthesis and their usage enforced as such, mis-matches in the AST (when creating macro's) by special casing things like 2-tuples among others, and the really weird multi-arity functions of `for` and `with` that really should have been done via body expressions instead of `,` separated expressions (and thus would not need to be special forms but could then just be normal macros, but they seem extremely out of place for the rest of the language syntax), etc.... etc... etc.. It's just a lot of little inconsistencies like that. Also, if you want pipes for erlang look at the https://github.com/rabbitmq/erlando https://github.com/rabbitmq/erlando parse transform (there are others as well, but I like this one), and if you want a shorter erlang form then look at erl2 (just a layer on top of erlang to shorten constructs like module definitions), and of course nothing beats `lfe` on the beam for succinctness and defineability (being a lisp after all). Also, your Elixir and Erlang codes do not do the same thing. Your Elixir code is being run at compile-time where your Erlang code is not executed at all, only compiled, and thus will only be run if something calls the `tok:start/0` function. The equivalent elixir would be (formatted by the Elixir formatter): ```elixir defmodule :tok do def start() do "Hello,How,Are,You,Today" |> String.split(",") |> Enum.join(".") |> IO.puts() end end ``` And you can leave off the final `:ok` on both as both `IO.puts/1` and `io:fwrite/2` both return the atom `ok` in the end. And even then these are not equal as you are working with charlists with one and binaries with the other, so a more direct translation of the Erlang code to Elixir would probably actually be: ```elixir defmodule :tok do def start() do 'Hello,How,Are,You,Today' |> :string.tokens(',') |> :string.join('.') |> (&:io.fwrite('~s~n', [&1])).() end end ``` That would make the functions equal. As an aside, a few other bits, Elixir defines if a function is public/private via `def`/`defp`, meaning you have to search through a file to see what is exposed, where in Erlang you can see exactly what is and everything that is exposed by just looking at the top. And yes, `do`/`end` may be longer by one line than Erlang's sequence operator `,` usage, but I don't mind it to be honest, and you can replicate it in Elixir anyway kind of like: ```elixir def start(), do: ( lst = :string.tokens('Hello,How,Are,You,Today', ','); :io.fwrite('~s~n', [:string.join(lst,',')]); :ok) ``` Essentially just replace erlang's `->` with `,do: (`, replace erlang's `,` with `;`, and finally replace erlang's `.` with `)`. Comparing 'function' differences between them is useless of course, they can call each other functions (though macro's are another issue, erlang cannot 'usefully' call elixir macro's, but then again elixir may not soon be able to use erlang's parse transforms either), the differences are the syntax, and Elixir just has a whole ton of oddities. Overall I find the Erlang syntax (which is similar to SML/OCaml/etc...) far more uniform, sensible, readable, and that it has far less surprising syntactical cases (like why can't I do something like `%{some_atom: 42, "string key" => 6.28}` in Elixir, blah... you have to do `%{"string key" => 6.28, some_atom: 42}` instead for who knows why reasons...). I do of course use Elixir for my day job, have for over 2 years now, and these are just a few constant things that bug me on a day-to-day basis. However the tooling, macro's, and community (come join us on the Elixir Forums!!!) are top-notch! If I had my 'choice' of mythical language though, I'd pick something like OCaml with Rust-style ownership semantics with a full staged macro system (Elixir's is a staged macro system) if it were not otherwise possible to safely add in a full Lisp-style macro system (I really don't think so in a full setup like these though, you need a staged system for various corner-case reasons).
- rs86 8y agoI think the AST is easier to see in erlang.
- pmontra 8y agoThis example captures extremely well what I don't like in Elixir: verbosity. I'm so much more happy writing the equivalent Ruby code: puts "Hello,How,Are,You,Today".split(",").join(".") It's value.method.method vs value |> Module.function |> Module.function. Alias is not a general solution because of conflicts. Disclaimer: I use Elixir in projects for customers and I like it. Still, it's too verbose and I'm not using it in my own projects (no need for heavy parallelism there.)
- mercer 8y agoInteresting. I found it immensely liberating to be able to fully separate the functions from the data they operate on. I can see how it might feel verbose though.
- pmontra 8y agoI don't feel liberated when I program in Elixir but the other languages I use for customers are Ruby, Python and JavaScript, which are quite liberal. It's been so long since I used Java that I can't really appreciate anymore how big the difference is. For sure I don't want to go back to those times. IMHO "this is a string" |> String.split(" ") doesn't fully separate the data from the function. It's String.split after all and it won't work with an input of any other type. However, at least in the case of languages with duck typing, some_object.split(" ") works with an object of type String and with any other class that implements a split method that takes a string as argument. Then the rest of the code must be able to keep handling the duck typed object. Probably this isn't the way you are thinking about data/code separation, still it's a kind of separation.
- mercer 8y agoWell, I came from Javascript/Ruby/Python (and still like those too), so for me it really was the functional nature of Elixir that felt 'liberating'. Using String.split() instead of an method call on the string is just one of the reasons I like this rather stricter separation between data and code, and I do see how in isolation it's not all that great. That said, I still enjoy the more OO languages and rather like using some of the FP things I've learned inside those. And sometimes it does feel convenient to not be pushed into one particular style of coding. So perhaps 'liberating' is not the best choice of words :).