[erlang-questions] Must and May convention

Richard A. O'Keefe ok@REDACTED
Fri Sep 29 04:45:01 CEST 2017



On 29/09/17 4:33 AM, Fred Hebert wrote:

> As an example, Elixir has added the 'With' syntax:
>
>    opts = %{width: 10, height: 15}
>    with {:ok, width} <- Map.fetch(opts, :width),
>         {:ok, height} <- Map.fetch(opts, :height),
>      do: {:ok, width * height}

How is this different from 'let'?

There was a very old proposal for Erlang that
variables introduced between 'begin' ... 'end'
brackets should be local, so that this would be
     begin
         {ok,Width}  = map:fetch(Opts, width),
         {ok,Height} = map:fetch(Opts, height),
         {ok, Width*Height}
     end
If memory serves me, this was proposed about 25 years
ago, at about the same time that 'cond' was.

>
> This is now some kind of new fancy syntax. However, Erlang lets you do
> something similar to with with list comprehensions:
>
>    Opts = dict:from_list([{width,10}, {heigth,15}]),
>    hd([{ok, Width*Height}
>        || {ok, Width} <- [dict:find(width, Opts)],
>           {ok, Height} <- [dict:find(height, Opts)]])

Or with
     (fun ({ok,Width}, {ok,Height}) -> {ok,Width*Height} end
     )(map:fetch(Opts, width), map:fetch(Opts, height))




More information about the erlang-questions mailing list