kecatan914

kecatan914

What is your opinion on effect handlers or the lack of in Elixir?

I’m studying more and more functional programming and Elixir is the language I liked the most. I would like to have static types, but I can leave without it. The biggest issue so far has been the fact that state can happen absolutely everywhere. Isn’t this a huge drawback in terms of reasoning and referencial transparency?

Most Liked

sodapopcan

sodapopcan

As many of us like to talk about, Erlang is not an academic language and is only functional because it needed to be. Many of us like to say “pragmatic” :slight_smile: So it doesn’t come with a lot of things that other functional languages have and it’s up to you do the right thing. Effectful functions will almost always (or, in the standard library I believe always) return a status tuple—usually {:ok, _}/{:error, _} which another user around here has affectionately referred to as a ghetto monad :smiley:) or have a ! at the end of its name which signals that it will raise in case of an error. But ya, there is no way to enforce this at compilation, unfortunately. It can definitely be a problem if you’re working with people who don’t know what they are doing. Usually we try and keep side effects at the same “layer” and something like boundary can help with this. It still won’t stop someone from calling File.write buried away in some poorly named private function but you can, for example, reasonably keep something like database interactions in check.

Another option is fairly new library called Efx. It’s primary purpose is for mocks but it requires that you define any function that needs to be mocked with defeffect. As it turns out, you could use defeffect for all effectful functions, even if you aren’t going to mock them. But again, this isn’t something enforced by the compiler.

As a positive, from a pure base language syntax point of view, Elixir is always immutable. We don’t any keywords to make a variable mutable. If we want state, we have to start a server! Or use the built in key/value store or some other such solution that isn’t as assigning a variable.

TL;DR, if you are comfortable with FP, it’s not really a problem. But ya, I do have first hand experience of it becoming a problem.

derek-zhou

derek-zhou

I think what you meant to say is side-effect can happen absolutely everywhere. State management is actually a strong suit for Elixir/Erlang.

Yes, Elixir and Erlang are not pure. It is a trade-off that the language designer carefully made. There are also Closure, Ocaml and Scala in different places on the pure-ness spectrum; try them and see how you like the trade-off they made.

garrison

garrison

Every function is referentially transparent if you’re brave enough!

In practice most of us are writing webapps where we hit a DB for our state anyway. Honestly other than that I very rarely write functions that aren’t pure.

It can happen, but in practice it rarely does. Have you actually run into problems here or is this more on the theory-crafting side?

Off the top of my head the only common examples of mutable state that I actually run into are:

  • :rand.seed/2
  • Plug CSRF protection stores the token in the process dictionary
  • Ecto transaction state (in a transaction or not) is implicit

And all of these are pretty valid reasons to abuse mutability IMO. There are certain (rare) situations where immutable functions are actually less safe because it would be easy to forget to pass something in. For example, one might forget to pass a seed to Enum.random() if it worked that way, causing subtle bugs. Likewise with the CSRF token, where such a mistake could be a serious vulnerability.

Where Next?

Popular in Questions Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

We're in Beta

About us Mission Statement