How to handle nested case

Gian Lorenzo Meocci glmeocci@REDACTED
Mon Sep 20 15:33:06 CEST 2021


Hi Fred,
this is a good news because it's generic and rooted in the language!

Do you have any timing estimation to have it in the main branch?
Thanks a lot

On Mon, 20 Sept 2021 at 15:24, Fred Hebert <mononcqc@REDACTED> wrote:

> This is timely, because we're trying to land EEP-49:
> https://github.com/erlang/otp/pull/5216
>
> Which would turn the original code into:
>
> get_nas_from_conf() ->
>   begin
>     {ok, Server} <- get_radius_host(),
>     {ok, Port} <- get_radius_port(),
>     {ok, Secret} <- get_radius_secret(),
>     {Server, Port, Secret}
>   cond
>     {error, _} = E -> E
>   end.
>
> The 'cond' keyword is still in the air (the EEP specified 'else' but we
> hit an implementation snag).
>
>
> On Mon, Sep 20, 2021 at 5:05 AM Jesper Louis Andersen <
> jesper.louis.andersen@REDACTED> wrote:
>
>> What about something like:
>>
>> get_nas_from_conf() ->
>>   {ok, Server} = get_radius_host(),
>>   {ok, Port} = get_radius_port(),
>>   {ok, Secret} = get_radius_secret(),
>>   {Server, Port, Secret}.
>>
>> Errors are now just crashes. But if the errors are really truly something
>> you want to handle, chances are you need some other flow anyway.
>>
>> On Sat, Sep 18, 2021 at 10:50 PM Gian Lorenzo Meocci <glmeocci@REDACTED>
>> wrote:
>>
>>> 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>
>>>
>>
>>
>> --
>> J.
>>
>

-- 
GL
https://www.meocci.it <https://meox.github.io>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210920/2331614f/attachment.htm>


More information about the erlang-questions mailing list