DEV Community

Cover image for An overview of Elixir from C# developer
Rafael Andrade
Rafael Andrade

Posted on

An overview of Elixir from C# developer

Recently, I discovered a programming language called Elixir. Elixir is described as a dynamic, functional language for building scalable and maintainable applications.

Syntax

Elixir has a syntax very similar to Ruby, to start a new block you will use do end, missing the return keyword (which confused me at the beginning) it always returns the last statement.

Immutability 

Another good feature that Elixir has is immutability, so every time you change an object, you clone it and change it, making it very safe for concurrency. Another tip is when you talk about the list, by default Elixir uses a linked list and you should pre-append the new data because pre-append is constant time ( I guess it's O(1)) and appending is slow (I guess it's O(N)).

Pattern matching

One of the most interesting features of Elixir is pattern matching, it allows you to execute a code based on parameter value, see:

def do_something(1), do: :foo
def do_something(0), do: :bar
def do_something(a) when is_integer(a) and a > 2 do: :foo2
def do_something(a) when is_integer(a) and a < 0, do: :ba2
def do_something(_a), do: :unknown
Enter fullscreen mode Exit fullscreen mode

Looking at the code above you can see depending on the input we will have a different output eliminating if inside the do_something, it's similar to C# method overload but much more powerful.

Dynamic Language

I'm not a big fan of dynamic languages because I don't know what to expect from the parameters, variables or properties, let imagine I have a method called DoSomething and it receives a parameter, what is the parameter type, should it be a string, integer and object. I have the impression that dynamic language doesn't have good developer experience. I don't have much experience with dynamic language, I worked with C# most of my time, and just a few times I worked with Javascript and Python. With this in mind, I liked the way how Elixir addressed this issue, they are implementing a type system where you add an annotation to your code and at the same time elixir tries to determine the type by context and by guards.

@spec do_something(integer()) :: { :ok }
def do_something(a) when is_integer(a) do
  { :ok }
end
Enter fullscreen mode Exit fullscreen mode

Beam VM

Elixir is built on top of Erlang/Beam VM, it was created by Ericsson to be scalable, fault-tolerant, highly available and hot-swape everything that I was looking for. In a high overview, Beam VM works with "process" (It's not an OS process, it's a light process that BEAM VM can create very quickly) and this process communicates with another process by sending a message and this allows Beam VM to send that message to another machine (or to other CPU) abstraction this for a developer, so, at the start you won't need a queue or a stream system to scale.

Conclusion 

Elixir has a lot of good features and the BEAM VM is very good too, unfortunately, it's not a mainstream language (maybe because it doesn't have a Big tech supporting it 🤔) and doesn't get enough attention as it should. I'll continue doing more articles about the Elixir.

Top comments (0)