Re: How to handle nested case

Andreas Schultz <[email protected]>
Newsgroups gmane.comp.lang.erlang.general
Message-ID <CAC=ZbaSF=2GPWKjNjq2nAQCPFoGMj9r4CeRV3QJ6O=d5J4Ph7w@mail.gmail.com>
Hi,

With the help of https://github.com/eryx67/erlando.git, you can write this
as:

-compile({parse_transform, do}).
-compile({parse_transform, cut}).


get_nas_from_conf() ->
    do([error_m ||
           Server <- get_radius_host(),
           Port <- get_radius_port(),
           Secret <- get_radius_secret(),
           return({Server, Port, Secret})
       ]).


This will return {ok, {Server, Port, Secret}} or {error, Error}.

Regards,
Andreas

Am Sa., 18. Sept. 2021 um 22:50 Uhr schrieb Gian Lorenzo Meocci <
[email protected]>:

> 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>
>


-- 

Andreas Schultz
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.