DimWell
February 21, 2018, 6:36am
1
Hi,
Is there any documentation or description of best practice on how to use the “with ” statement ?
Or at least proper syntax description ?
Using some examples from online and this forum, I wrote the below case.
The problem is that when I run an app I see that something is going on, and then nothing, no success result and no errors.
Simply, empty terminal. Any suggestions of what could be wrong ?
with {:ok, url} <- do_get_url(id, lang, url),
{:ok, json} <- do_get_json(url),
{:ok, map} <- do_convert_json_to_map(json),
{:ok, models} <- do_get_models(map, amount),
{:ok, flat_map} <- do_flatten_map(models),
{:ok, models} <- do_download_model_images(flat_map, %{download_dir: dir, image_max_size: size})
do
{:ok, models}
else
{:error, reason} -> {:error, reason}
error -> {:error, "#{inspect error}"}
end
1 Like
idi527
February 21, 2018, 7:17am
2
So just to be clear, you get neither {:ok, models}
nor {:error, reason}
back?
voger
February 21, 2018, 9:16am
3
except the last statement error -> {:error, "#{inspect error}"}
is anything else supposed to print something on the terminal?
Maybe try
with {:ok, url} <- do_get_url(id, lang, url),
{:ok, json} <- do_get_json(url),
{:ok, map} <- do_convert_json_to_map(json),
{:ok, models} <- do_get_models(map, amount),
{:ok, flat_map} <- do_flatten_map(models),
{:ok, models} <- do_download_model_images(flat_map, %{download_dir: dir, image_max_size: size})
do
{:ok, models} |> IO.inspect
else
{:error, reason} -> {:error, reason} |> IO.inspect
error -> {:error, "#{inspect error}"}
end
Also in this line
{:ok, models} <- do_get_models(map, amount),
the amount
variable is declared somewhere above the with
statement?
1 Like
DimWell
February 21, 2018, 11:40am
4
Hi, thank your for your replays!
I did check step by step the code, and the problem was in one do_flatten_map(models),
There, was guard is_map(models) ,but by mistake the list was passed to this function.
Therefore, the pattern matching did work. The strange thing is that there was no errors…
Currently the problem solved, but the main question was about documentation - is there any official documentation about:
with
statement ?
https://hexdocs.pm/elixir/Kernel.SpecialForms.html#with/1
Just by way of education with
, like basically everything in Elixir, is an expression not a statement. Expressions return values statements do not. An example of the latter would be a C style if block.
5 Likes
DimWell
February 21, 2018, 12:04pm
6
Hi benwilson512 ,
Thank you for clarification and for the link.
This is again exactly that I needed !