Kandis.KdHelpers.Maybe (kandis v0.5.12)

Access nested structs, protected from nil. See maybe/1 or maybe/2 for more details.

Example

import KandisHelpers.Maybe

map = %{city: %{name: "Portland"}}
maybe(map, [:city, :name]) # => "Portland"
maybe(map.city.name) # => "Portland"
maybe(map.city.location) # => nil

Summary

Functions

Get a value out of a nested map or struct, or return nil. Compiles down to maybe/2. In other words, this

Get a value out of a nested map or struct, or return nil. For prettier syntax, see the maybe/1 macro.

Functions

Link to this macro

maybe(ast)

(macro)

Get a value out of a nested map or struct, or return nil. Compiles down to maybe/2. In other words, this:

maybe(map.city.name)

Is compiled down to a simple function call to maybe/2:

maybe(map, [:city, :name])

Examples

iex> map = %{city: %{name: "Portland"}}
...> maybe(map.city.name)
"Portland"

iex> map = %{city: nil}
...> maybe(map.city.name)
nil
Link to this function

maybe(val, arg2)

@spec maybe(map(), [atom()]) :: any() | nil

Get a value out of a nested map or struct, or return nil. For prettier syntax, see the maybe/1 macro.

Examples

iex> map = %{city: %{name: "Portland"}}
...> maybe(map, [:city, :name])
"Portland"

iex> maybe(%{}, [:city, :name])
nil