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/616a2726-740d-4b10-a02c-2a0c4e195a2fn%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/elixir-lang-core/616a2726-740d-4b10-a02c-2a0c4e195a2fn%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>
--
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/27c71bfd-0e3d-49ef-91a3-67383e8ef3fdn%40googlegroups.com.