erlang - Module properties Elixir -
i new in elixir/erlang programming.
how can implement module attributes elixir module, user of module can set in module constructor.
for example,
defmodule config some_property: nil other_property: nil def constructor(one, two) some_property = 1 other_property = 2 end def get_property_one some_property end end
thank you
elixir modules not classes , variables defined within them not properties, there no such things constructors , destructors. elixir based on erlang, firstly, recommend reading erlang , object oriented programming:
why oo sucks joe armstrong object oriented programming: wrong path? why programme in erlangthis should give basic idea, why objects getters , setters aren't supported. closest thing having object state have server state. in server loop, can this:
def loop(state) newstate = receive { :get_property_one, pid } -> pick_the_property_from_state_and_send_to_pid(state, pid) state { :set_property_one } -> set_property_one_and_return_new_state(state) end loop(newstate) end
spawning new server initial state close createing new object using constructor. sending :get_property_one
getter, asynchronous (you can else before waiting reply). sending :set_property_one
doesn't wait reply, non blocking.
this might cumbersome, solves couple of problems:
you not have readers, writers problem in erlang, because requests processed 1 one if getters , setters require complex operations, can done asynchronously (without blocking calling process)this pattern common, there behaviour
called gen_server
, eliminates of boilerplate of looping. if still think, there much boilerplate write, can read my article it (this 1 in erlang)
erlang elixir
No comments:
Post a Comment