4 ms·
> They seem to have done a lot of things right'' This is because Elixir is "just" a different syntax for Erlang/OTP which is used in very mission critical soft
by mikkom 11y ago
> They seem to have done a lot of things right''
This is because Elixir is "just" a different syntax for Erlang/OTP which is used in very mission critical software in real world (it was originally created for telephone systems)
- rdtsc 11y agoTo be fair, it is not just a different syntax. It can do a few things a bit better: * Streams * Actual macros * Mutable variables (not data, of course) Those would be kind of a pain to do in Erlang, maybe with parse transforms. I personally prefer Erlang better. I like its syntax and immutable variables. Haven't hit a need to need macros too badly yet.
- lectrick 11y agoWhere does Elixir allow mutable variables? AFAIK, it does not. Syntactically it might look like it allows mutable variables, but it actually does not.
- rdtsc 11y ago> Where does Elixir allow mutable variables? Everywhere pretty much. Learn a bit more how it works. Here is a good resource: http://elixir-lang.org/getting-started/introduction.html http://elixir-lang.org/getting-started/introduction.html Here is an example: iex(1)> x=1 1 iex(2)> x=2 2 x was 1, then it was 2. It mutated. To compare here is Erlang: 1> X=1. 1 2> X=2. ** exception error: no match of right hand side value 2 Just like in math, once X=1, it stays that. It doesn't get to mutate.
- nightwolf 11y agoThe variable isn't mutated, actually — it's rebound. See http://natescottwest.com/elixir-for-rubyists-part-2/ http://natescottwest.com/elixir-for-rubyists-part-2/, for example.
- rdtsc 11y agoYes it is. x is the variable. It changed from 1 to 2. Mutation is a synonym for change. Try it yourself in the interpreter! So data is immutable, but variables are mutable in Elixir.
- andruby 11y agoBut if you passed that first x to a process, it would still be 1 even after you ran `x = 2`.
- lectrick 11y agoDude that's not a mutation that's a rebinding. You do not understand the difference, and it is an important one. Again, Elixir does not create mutation where in Erlang there was none. There is no mutation. They decided to allow rebinding so that you wouldn't end up with a whole bunch of stupid temporary variables, which is exactly what you have to do in Erlang.
- rdtsc 11y agoThat is a variable mutation. Mutation is a change in value. x was 1 then it was 2. If that is not mutation, I don't know what is. You are probably trying to say that Elixir and Erlang have immutable data. But that's what that's called "immutable data". Precisely because the variables can be mutated or not separately from the data. > a whole bunch of stupid temporary variables, which is exactly what you have to do in Erlang. Well we should tell Joe that it's stupid. Maybe he'll change his mind.
- lectrick 11y agoIf variables don't mutate, all it means is that the memory location that stores the value of that variable is guaranteed to never change. Thus, if you reassign that same-named variable, it actually points to a new memory location with the new value. That is what "rebinding" is vs. "mutating." The advantage of this is concurrency, etc. etc. The disadvantage is you have to have good GC since you'll be doing a lot more garbage collection. It may LOOK like it's changing a variable, but it's not- the variable merely points to a new memory location. Other languages, like Ruby, do not work this way. If you reassign an instance variable, the memory that is storing that value actually does change. Here, compare the same sequence of operations in Ruby and Elixir, first Ruby: bash>> irb >> a = {a: 5} => {:a=>5} >> b = a => {:a=>5} >> a[:a] = 10 => 10 >> b => {:a=>10} and now Elixir: bash>> iex Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace] Interactive Elixir (1.1.0) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> a = %{a: 5} %{a: 5} iex(2)> b = a %{a: 5} iex(3)> a = %{a | a: 10} # updates a mapping %{a: 10} iex(4)> b %{a: 5} Notice how b's value changed in Ruby, but not in Elixir. In both cases, the hash was updated.