Sha256: 0ac5ca169652e72a32b8c9bf5a94e0fabfba5f17f42fa5a3c74b9436aaf04d17

Contents?: true

Size: 1.34 KB

Versions: 28

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

Puppet::Parser::Functions.newfunction(
  :lest,
  :type => :rvalue,
  :arity => -2,
  :doc => <<~DOC
    Call a [lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html)
    (which should accept no arguments) if the argument given to the function is `undef`.
    Returns the result of calling the lambda if the argument is `undef`, otherwise the
    given argument.

    The `lest` function is useful in a chain of `then` calls, or in general
    as a guard against `undef` values. The function can be used to call `fail`, or to
    return a default value.

    These two expressions are equivalent:

    ```puppet
    if $x == undef { do_things() }
    lest($x) || { do_things() }
    ```

    **Example:** Using the `lest` function

    ```puppet
    $data = {a => [ b, c ] }
    notice $data.dig(a, b, c)
     .then |$x| { $x * 2 }
     .lest || { fail("no value for $data[a][b][c]" }
    ```

    Would fail the operation because $data[a][b][c] results in `undef`
    (there is no `b` key in `a`).

    In contrast - this example:

    ```puppet
    $data = {a => { b => { c => 10 } } }
    notice $data.dig(a, b, c)
     .then |$x| { $x * 2 }
     .lest || { fail("no value for $data[a][b][c]" }
    ```

    Would notice the value `20`

    * Since 4.5.0
  DOC
) do |_args|
  Puppet::Parser::Functions::Error.is4x('lest')
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
puppet-8.10.0 lib/puppet/parser/functions/lest.rb
puppet-8.10.0-x86-mingw32 lib/puppet/parser/functions/lest.rb
puppet-8.10.0-x64-mingw32 lib/puppet/parser/functions/lest.rb
puppet-8.10.0-universal-darwin lib/puppet/parser/functions/lest.rb
puppet-8.9.0 lib/puppet/parser/functions/lest.rb
puppet-8.9.0-x86-mingw32 lib/puppet/parser/functions/lest.rb
puppet-8.9.0-x64-mingw32 lib/puppet/parser/functions/lest.rb
puppet-8.9.0-universal-darwin lib/puppet/parser/functions/lest.rb
puppet-8.8.1 lib/puppet/parser/functions/lest.rb
puppet-8.8.1-x86-mingw32 lib/puppet/parser/functions/lest.rb
puppet-8.8.1-x64-mingw32 lib/puppet/parser/functions/lest.rb
puppet-8.8.1-universal-darwin lib/puppet/parser/functions/lest.rb
puppet-8.7.0 lib/puppet/parser/functions/lest.rb
puppet-8.7.0-x86-mingw32 lib/puppet/parser/functions/lest.rb
puppet-8.7.0-x64-mingw32 lib/puppet/parser/functions/lest.rb
puppet-8.7.0-universal-darwin lib/puppet/parser/functions/lest.rb
puppet-8.6.0 lib/puppet/parser/functions/lest.rb
puppet-8.6.0-x86-mingw32 lib/puppet/parser/functions/lest.rb
puppet-8.6.0-x64-mingw32 lib/puppet/parser/functions/lest.rb
puppet-8.6.0-universal-darwin lib/puppet/parser/functions/lest.rb