3 ms·
I've worked heavily with Go for several years now but recently I have been transitioning to Elixir. In my opinion, Elixir handles this kind of error return pro
by solid_fuel 3y ago
I've worked heavily with Go for several years now but recently I have been transitioning to Elixir. In my opinion, Elixir handles this kind of error return problem better with two separate patterns:
1) Somewhat similar to what the author of this post achieved, there is a pattern commonly used in Elixir libraries where there are often variants of library functions which end with ! which raise an exception when a problem is encountered.
For example, the file module https://hexdocs.pm/elixir/1.13/File.html#read/1 https://hexdocs.pm/elixir/1.13/File.html#read/1 provides multiple ways to read a file:
File.Read() -> returns {:ok, <<content>>} or {:error, <<reason>>}
File.Read!() -> returns <<content>> or raises an exception
2) Pattern matching is also a common way to deal with this.
# This will raise a pattern matching exception if something other than :ok is returned from File.Read
{:ok, content} = File.Read(...)
Pattern matching like this isn't supported in Golang but I think it would be fantastic to be able to write
zd, nil := gzip.NewReader()
and have that panic if an error was returned instead.
- sethammons 3y agoI like a lot about elixir, including what you point out. Pattern matching is awesome. Its type hinting via spec is not enough (i hear some developments are on their way here). I don't like that I often had to jump up several callers to understand the arguments coming into my function. Go wins here. And I also require performance. While it may have been query abuse by Ecto, the Elixir code base I was in was only able to handle like 3k rps across 5 nodes. I expect nearly 2x that from a single similarly sized Go node making Go 10x more performant in naive implementations. Easier to read and more performant? I chose Go. It also plays nicer with K8s; we had trouble getting nodes linked up in elixir to allow multi node BEAM features.