View Source Bonfire.Common (Bonfire v0.9.10-classic-beta.156)

A library of common utils and helpers used across Bonfire extensions.

An extension for Bonfire that contains:

Handy commands

Copyright (c) 2020 Bonfire, VoxPublica, and CommonsPub Contributors

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.

Summary

Functions

Provides a fallback value or function when the first argument is nil.

Functions

Link to this function

maybe_fallback(val, fun)

View Source

Provides a fallback value or function when the first argument is nil.

  • If the first argument is not nil, returns the first argument as is.
  • If both arguments are nil, returns nil.
  • If the first argument is nil and the second argument is a function, calls the function and returns its result.
  • If the first argument is nil and the second argument is not a function, returns the second argument as is.

Examples

iex> maybe_fallback("value", "fallback value")
"value"

iex> maybe_fallback(nil, nil)
nil

iex> maybe_fallback(nil, fn -> 1+2 end)
3

iex> maybe_fallback(nil, "fallback value")
"fallback value"