Re: How to handle nested case

Attila Rajmund Nohl <[email protected]>
Newsgroups gmane.comp.lang.erlang.general
Message-ID <CAFkcMa_98qKcm4EQSPHu9zqG5XPrTMW6R+kVeAQyCNAr9f9vVw@mail.gmail.com>
Hello!

The functions *are* executed conditionally - if get_radius_host()
doesn't return {ok, Server}, then get_radius_port() won't be called.

Michael Malter <[email protected]> ezt írta (időpont: 2021. szept.
19., V, 10:08):
>
> Hi
>
> Now it's kinda ugly.
> You could just keep the lines from your try clauses and put the result in a case or a function.
>
> Anyway, the execution flow is different now. I think OP wanted the get functions executed conditionaly.
>
> I will repeat, just put the cases in functions.
>
> Michaël Malter
>
> Le dim. 19 sept. 2021 à 09:07, Attila Rajmund Nohl <[email protected]> a écrit :
>>
>> Hello!
>>
>> One way (I haven't actually tried to compile this, but hopefully you
>> get the idea):
>>
>> get_nas_from_conf() ->
>>   try
>>     {ok, Server} = get_radius_host(),
>>     {ok, Port} = get_radius_port(),
>>     {ok, Secret} = get_radius_secret(),
>>     {Server, Port, Secret};
>>   catch
>>     error:{badmatch, E} ->
>>       E
>> end.
>>
>> Of course, if the badmatch comes from the get_* functions, this
>> construct might catch that too, but you might be able to  ensure this
>> doesn't happen.
>>
>> Gian Lorenzo Meocci <[email protected]> ezt írta (időpont: 2021.
>> szept. 18., Szo, 22:50):
>> >
>> > 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
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.