Modifying State in Elixir

Does Elixir have a default return value when it modifies state? I ask because languages like OCaml use the type unit to indicate its modifying state. I was wonder what’s the convention in Elixir when something modifies state.

So, it took a few readings to understand the question. Let me see if I actually have.

You’re saying that in OCaml the type system can track when you have a function which has a side effect, and you’re wondering if there is an analogous equivalent in Elixir?

While in elixir all data is immutable it can have side effects via message passing and side causes, by receiving messages. Elixir is a dynamic language, and thus does not have a compile time enforced type system. Typespecs can be used by way of documentation, and some degree of static analysis exists via dialyzer, but that’s all it has in that department.

Basically, I think the answer is “no”. Elixir does not have a type system which indicates when a function has a side effect.

Although it can track that (it marks the function and up to the module as imppure instead of pure), that is not what it does. Rather OCaml has the () type, think of it as the empty tuple in Elixir (as that is really what it is in OCaml), and has the type unit. By convention (though not required), any function that mutates something or has side-effects generally returns unit, thus making you do something like let () = doMutation ..., which makes it obvious where side-effects are in code by easily looking for ()'s.

This is correct, Elixir does not, however the upcoming mlfe might eventually (help work on it!).

1 Like

If it is really used like this, then I have to say, that I have read similar in Elixir and Erlang code: _ = do_effect_full_operation to explicitely denote that you call a function because of its side effects.

1 Like

True, although many also use that form to silence dialyzer and not necessarily to state that it is side-effecting. ^.^

To not use a return value does only make sense if that operation is effectfull. If the function is pure, and you do not use the return value, you do actually only burn time (which you could consider just another wanted or unwanted sideeffect).

Yep, in OCaml you could do let _ = blah too but I’ve not seen it. :slight_smile:

I like that _ = do_side_effects convention just as much as I dislike calling functions solely for their side-effects, so I’ll have to remember that one.

seems like a good convention though. if you only had do_side_effects on a line by itself you might just think it’s a macro, so a descriptive name and a _ match seem like a good idea.

1 Like

The reason you haven’t seen it is developers like to use let () to help the compiler with type checking.

let () = print_endline “Hello, World!”

Yep, that was part of my point, explicit is better than implicit. :slight_smile:

1 Like

That is more a “problem” with macros. I prefer just view the call/construction that if I don’t do anything with the return value, which you always get, then you are calling it for the side effect. A bit ambiguous if the it is the tail of a function though.

But you do run into philosophical problems if you export variables from if/case/receive which is frowned upon.