We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Computing map defaults only when needed
almirsarajcic
0 comments
Copy link
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:
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.
copied to clipboard