How to handle nested case

Stanislav Ledenev s.ledenev@REDACTED
Sun Sep 19 11:21:45 CEST 2021


Rewrite your get_* functions to throw and do smth. like this:

get_nas_from_conf() ->
    try
        {ok, {get_radius_host(), get_radius_port(), get_radius_secret()}}
    catch
        error:E ->
            {error, E}
    end.

And don't worry, throw is the simple goto in Erlang.


сб, 18 сент. 2021 г. в 23:50, Gian Lorenzo Meocci <glmeocci@REDACTED>:

> Hi,
> I have a function like this:
>
> get_nas_from_conf() ->
> case get_radius_host() of
> {ok, Server} ->
> case get_radius_port() of
> {ok, Port} ->
> case get_radius_secret() of
> {ok, Secret} ->
> {Server, Port, Secret};
> E -> E
> end;
> E ->
> E
> end;
> E ->
> E
> end.
>
> Which is the best way to write this kind of function?
>
> I'd like to have a with operator like in Elixir, to rewrite my function in
> this way:
>
> get_nas_from_conf() ->
> with {ok, Server} <- get_radius_host(),
> {ok, Port} = get_radius_port(),
> {ok, Secret} = get_radius_secret() ->
> {Server, Port, Secret};
> catch
> {error, _Reason} = E ->
> E
> end.
>
> Any suggestion?
>
> --
> GL
> https://www.meocci.it <https://meox.github.io>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210919/ea6bc870/attachment.htm>


More information about the erlang-questions mailing list