Elixir modules are not classes and variables defined inside them are not properties, so there are no such things as constructors and destructors. Elixir is based on Erlang, so firstly, I would recommend some reading about Erlang and object oriented programming:
This should give you the basic idea, why objects with getters and setters aren't supported. The closest thing to having an object with state is to have a server with state. So in server loop, you can do something like this:
def loop(state) do
newstate = receive do
{ :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 with initial state is close to createing new object using constructor. Sending :get_property_one is like getter, but asynchronous (you can do something else before waiting for reply). Sending :set_property_one doesn't wait for reply, so it is non blocking.
This might look cumbersome, but it solves couple of problems:
- you will not have readers, writers problem in Erlang, because all the requests are processed one by one
- if your getters and setters require some complex operations, they can be done asynchronously (without blocking the calling process)
This pattern is so common, that there is behaviour called gen_server, that eliminates most of the boilerplate of looping. If you still think, that there is too much boilerplate to write, you can read my article about it (this one is in Erlang)