# Computing map defaults only when needed

If your app takes a region from its options and falls back to an environment variable, `Map.get/3` can raise even when the region is already set:

```elixir
iex> opts = %{region: "eu-west-1"}
iex> default_region = fn -> System.fetch_env!("AWS_REGION") end

# With AWS_REGION unset:
iex> Map.get(opts, :region, default_region.())
** (System.EnvError) could not fetch environment variable "AWS_REGION" because it is not set

iex> Map.get_lazy(opts, :region, default_region)
"eu-west-1"
```

`Map.get_lazy/3` calls `default_region` only when the key is missing. With `Map.get/3`, the fallback runs before the lookup because it’s a function argument.

A key set to `nil` is still present, so `Map.get_lazy/3` returns `nil` without calling the fallback.

https://hexdocs.pm/elixir/Map.html#get_lazy/3

---

Created by: almirsarajcic
Date: November 03, 2026
URL: https://elixirdrops.net/d/EqXuV1a8
