It would be cool to have the possibility to be able to use exceptions as
control flow for successful GenServer responses
I'm sure there is the opinion that this is anti-pattern, but having the
possibility could really simplify some handler functions scenarios where
you need to validate a lot of different entries
Having guard clauses to validate things could help
```
if a > b, do: reply! :some_error, state
```
In practice it would be something like this:
------
# We could have a GenResponse module that acts as a Exception
```
defmodule GenResponse do
defexception [:res, :new_state, :value, :extra]
end
# All GenServer handle functions would need to have a exception wrapper in
compile time, this could maybe be configured with something like:
# > use GenServer, exception_flow: true
try do
# Could be used directly like this
raise GenResponse, res: :noreply, new_state: :any
# Or also like this
noreply! :any
rescue
# Essentially, in every handle function something like this would be
under the hood
res in GenResponse ->
case res do
%GenResponse{res: :noreply, new_state: state} ->
{:noreply, state}
%GenResponse{res: :reply, value: value, new_state: state} ->
{:reply, value, state}
end
end
```
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/516d8516-ef81-4e73-8265-0b455a34f6bcn%40googlegroups.com.