Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Maybe one of the next BEAM languages will handle that automatically for us.

It not being automatic is a feature as that pattern is only what you want in the trivial case, but in real programs you are going to want more control.

In early Elixir there was a relatively popular macro library[1] that offered what you ask for (see code example below) but as Elixir matured and started being used in more than toy projects it was largely abandoned.

    defmodule Calculator do
      use ExActor.GenServer

      defstart start_link, do: initial_state(0)

      defcast inc(x), state: state, do: new_state(state + x)
      defcast dec(x), state: state, do: new_state(state - x)

      defcall get, state: state, do: reply(state)

      defcast stop, do: stop_server(:normal)
    end
[1]: https://github.com/sasa1977/exactor


Great. That was basically all we used of GenServers in the Elixir projects I worked on. I remember one or two handle_info but nearly all our GenServers were (in OO parlance) objects with methods to update their status, trigger actions and sometimes read the status.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: