This can be accomplished by using a `with` in which the `else` clause is the
good result. Readability is up for debate of course.
with :error <- Map.fetch(states, abbreviation), do: {:error, "State not found"}
-Greg Vaughn
> On Jan 26, 2021, at 3:50 PM, Jonathan Arnett <[email protected]> wrote:
>
> One potential benefit to a Map.fetch/3 command that wasn't considered is that
> it could help users define more specific error types. There's a loose
> convention to use {:ok, val} | :error | {:error, reason} as a "result" type
> in Elixir (analogous to Rust's Option and Result or Haskell's Maybe and
> Either). Map.fetch/3 conforms to this by returning {:ok, val} | :error which
> is very useful!
>
> However, sometimes I want to return a more specific error from Map.fetch
> failure. Consider the following case:
>
> states = %{"AL" => "Alabama", "AK" => "Alaska", "AR" => "Arkansas", ... }
> Map.fetch(states, abbreviation)
>
> This will return either {:ok, name} or :error. However, for the caller's
> convenience, I would return {:ok, name} or {:error, "State not found!"}.
>
> Map.get/2 could be used:
>
> case Map.get(states, abbreviation) do
> nil -> {:error, "State not found!"}
> name -> {:ok, name}
> end
>
> or Map.fetch/2:
>
> case Map.fetch(states, abbreviation) do
> {:ok, name} -> {:ok, name}
> :error -> {:error, "State not found!"}
> end
>
> But I would truly like to have a Map.fetch/3 function where the third
> argument is alternate default value (a la Map.get/3)
>
> Map.fetch(states, abbreviation, {:error, "State not found!"})
>
>
> On 1/26/21 3:53 AM, Paweł Urbanek wrote:
>> Thanks, I was not aware of that.
>>
>> On Tuesday, January 26, 2021 at 9:48:23 AM UTC+1 [email protected] wrote:
>> Hello,
>> I think you're looking for `.get_lazy/3` functions.
>>
>> Best regards,
>> Jean
>>
>>
>> Le mardi 26 janvier 2021 à 00:47 -0800, Paweł Urbanek a écrit :
>>> Looks like get does not evaluate the fallback function:
>>>
>>> Keyword.get([{:a, 1}], :b, fn () -> 2 + 2 end) => #Function<45.97283095/0
>>> in :erl_eval.expr/5>
>>>
>>> Maybe fetch!/3 could work like that?
>>>
>>> Keyword.fetch!([{:a, 1}], :b, fn () -> 2 + 2 end) => 4
>>> On Tuesday, January 26, 2021 at 9:40:13 AM UTC+1 [email protected] wrote:
>>>> Hello,
>>>>
>>>> There is Map.get/3 already, how would this differ?
>>>>
>>>> Best
>>>>
>>>> Adam
>>>>
>>>> On Tue, 26 Jan 2021 at 08:38, Paweł Urbanek <[email protected]>
>>>> wrote:
>>>>>
>>>>> I'd like to propose extending the API for Keyword and Map, fetch! method:
>>>>>
>>>>> Map.fetch!(%{a: 1}, :b, 2) => 2
>>>>> Keyword.fetch!([a: 1], :b, 2) => 2
>>>>>
>>>>> Third argument represents value which will be returned in case the
>>>>> requested key is missing. Optionally maybe it
>>>>> could also accept the function which is executed only in case the key is
>>>>> missing and it's return value is used.
>>>>>
>>>>> It would allow a simple way to fallback to default option values.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "elixir-lang-core" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/elixir-lang-core/eec03b82-3c23-4d56-a930-de1077913782n%40googlegroups.com.
>
> --
> You received this message because you are subscribed to the Google Groups
> "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/4ff59ad2-bda9-3353-494a-9de0d1c05e78%40gmail.com.
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/6BAD67CC-C1AB-4E87-9D73-8F96B81BA900%40gmail.com.