<div dir="auto">I think that there isn't a generic solution for this problem. It’s time to introduce the 'with' operator in Erlang 😂</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il Dom 19 Set 2021, 09:06 Attila Rajmund Nohl <<a href="mailto:attila.r.nohl@gmail.com">attila.r.nohl@gmail.com</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello!<br>
<br>
One way (I haven't actually tried to compile this, but hopefully you<br>
get the idea):<br>
<br>
get_nas_from_conf() -><br>
  try<br>
    {ok, Server} = get_radius_host(),<br>
    {ok, Port} = get_radius_port(),<br>
    {ok, Secret} = get_radius_secret(),<br>
    {Server, Port, Secret};<br>
  catch<br>
    error:{badmatch, E} -><br>
      E<br>
end.<br>
<br>
Of course, if the badmatch comes from the get_* functions, this<br>
construct might catch that too, but you might be able to  ensure this<br>
doesn't happen.<br>
<br>
Gian Lorenzo Meocci <<a href="mailto:glmeocci@gmail.com" target="_blank" rel="noreferrer">glmeocci@gmail.com</a>> ezt írta (időpont: 2021.<br>
szept. 18., Szo, 22:50):<br>
><br>
> Hi,<br>
> I have a function like this:<br>
><br>
> get_nas_from_conf() -><br>
> case get_radius_host() of<br>
> {ok, Server} -><br>
> case get_radius_port() of<br>
> {ok, Port} -><br>
> case get_radius_secret() of<br>
> {ok, Secret} -><br>
> {Server, Port, Secret};<br>
> E -> E<br>
> end;<br>
> E -><br>
> E<br>
> end;<br>
> E -><br>
> E<br>
> end.<br>
><br>
> Which is the best way to write this kind of function?<br>
><br>
> I'd like to have a with operator like in Elixir, to rewrite my function in this way:<br>
><br>
> get_nas_from_conf() -><br>
> with {ok, Server} <- get_radius_host(),<br>
> {ok, Port} = get_radius_port(),<br>
> {ok, Secret} = get_radius_secret() -><br>
> {Server, Port, Secret};<br>
> catch<br>
> {error, _Reason} = E -><br>
> E<br>
> end.<br>
><br>
> Any suggestion?<br>
><br>
> --<br>
> GL<br>
> <a href="https://www.meocci.it" rel="noreferrer noreferrer" target="_blank">https://www.meocci.it</a><br>
</blockquote></div>