Por que Elixir?

Thomaz Leite

Parte 2

Runtime

Erlang

Elixir

Tolerância a Erros

Distribuição

Concorrência

Polimorfismo

Metaprogramação

Unicode

Objetivos

Extensibilidade

Produtividade

Compatibilidade

Polimorfismo

Processes

Modules

Data/Type

Protocols

defprotocol Size do
  @doc "Calculates the size of a data structure"
  def size(data)
end

Protocols

defimpl Size, for: BitString do
  def size(string), do: byte_size(string)
end

defimpl Size, for: Map do
  def size(map), do: map_size(map)
end

defimpl Size, for: Tuple do
  def size(tuple), do: tuple_size(tuple)
end

Protocols

iex> Size.size("foo")
3
iex> Size.size({:ok, "hello"})
2
iex> Size.size(%{label: "some label"})
1

Protocols

defmodule User do
  defstruct [:name, :age]
end

defimpl Size, for: User do
  def size(_user), do: 2
end

Metaprogramação

Abstract Syntax Tree

Quoting & Unquoting

iex> quote do: sum(1, 2, 3)
{:sum, [], [1, 2, 3]}

Quoting & Unquoting

iex> number = 13
iex> Macro.to_string(quote do: 11 + number)
"11 + number"

Quoting & Unquoting

iex> number = 13
iex> Macro.to_string(quote do: 11 + unquote(number))
"11 + 13"

Macros

defmacro unless(clause, do: expression) do
  quote do
    if(!unquote(clause), do: unquote(expression))
  end
end

Mix, Hex & Docs

Mix

$ mix new foo
$ cd foo
$ mix test
$ mix hex.publish
$ mix hex.docs

Hex

Docs

Docs

defmodule KVServer.Command do
  @doc ~S"""
  Parses the given `line` into a command.

  ## Examples

      iex> KVServer.Command.parse "CREATE shopping\r\n"
      {:ok, {:create, "shopping"}}

  """
  def parse(_line) do
    :not_implemented
  end
end

Elixir ⬌ Erlang

1> inet_res:lookup("erlang.com", in, mx).
[{20,"mail.erlang.com"}]
iex(1)> :inet_res.lookup('erlang.com', :in, :mx)
[{20, 'mail.erlang.com'}]

DailyDrip

dailydrip.com

ELIXIR-POA-SETEMBRO

Made with Slides.com