stdlib_optval.fypp Source File


This file depends on

sourcefile~~stdlib_optval.fypp~~EfferentGraph sourcefile~stdlib_optval.fypp stdlib_optval.fypp sourcefile~stdlib_kinds.f90 stdlib_kinds.f90 sourcefile~stdlib_optval.fypp->sourcefile~stdlib_kinds.f90

Files dependent on this one

sourcefile~~stdlib_optval.fypp~~AfferentGraph sourcefile~stdlib_optval.fypp stdlib_optval.fypp sourcefile~stdlib_stats_corr.fypp stdlib_stats_corr.fypp sourcefile~stdlib_stats_corr.fypp->sourcefile~stdlib_optval.fypp sourcefile~stdlib_error.f90 stdlib_error.f90 sourcefile~stdlib_stats_corr.fypp->sourcefile~stdlib_error.f90 sourcefile~stdlib_stats_moment_all.fypp stdlib_stats_moment_all.fypp sourcefile~stdlib_stats_moment_all.fypp->sourcefile~stdlib_optval.fypp sourcefile~stdlib_stats_moment_all.fypp->sourcefile~stdlib_error.f90 sourcefile~stdlib_stats_var.fypp stdlib_stats_var.fypp sourcefile~stdlib_stats_var.fypp->sourcefile~stdlib_optval.fypp sourcefile~stdlib_stats_var.fypp->sourcefile~stdlib_error.f90 sourcefile~stdlib_stats_moment_mask.fypp stdlib_stats_moment_mask.fypp sourcefile~stdlib_stats_moment_mask.fypp->sourcefile~stdlib_optval.fypp sourcefile~stdlib_stats_moment_mask.fypp->sourcefile~stdlib_error.f90 sourcefile~stdlib_error.f90->sourcefile~stdlib_optval.fypp sourcefile~stdlib_logger.f90 stdlib_logger.f90 sourcefile~stdlib_logger.f90->sourcefile~stdlib_optval.fypp sourcefile~stdlib_io.fypp stdlib_io.fypp sourcefile~stdlib_io.fypp->sourcefile~stdlib_optval.fypp sourcefile~stdlib_io.fypp->sourcefile~stdlib_error.f90 sourcefile~stdlib_stats_cov.fypp stdlib_stats_cov.fypp sourcefile~stdlib_stats_cov.fypp->sourcefile~stdlib_optval.fypp sourcefile~stdlib_stats_cov.fypp->sourcefile~stdlib_error.f90 sourcefile~stdlib_stats_moment_scalar.fypp stdlib_stats_moment_scalar.fypp sourcefile~stdlib_stats_moment_scalar.fypp->sourcefile~stdlib_optval.fypp sourcefile~stdlib_stats_moment_scalar.fypp->sourcefile~stdlib_error.f90 sourcefile~stdlib_stats_mean.fypp stdlib_stats_mean.fypp sourcefile~stdlib_stats_mean.fypp->sourcefile~stdlib_optval.fypp sourcefile~stdlib_stats_mean.fypp->sourcefile~stdlib_error.f90 sourcefile~stdlib_stats_moment.fypp stdlib_stats_moment.fypp sourcefile~stdlib_stats_moment.fypp->sourcefile~stdlib_optval.fypp sourcefile~stdlib_stats_moment.fypp->sourcefile~stdlib_error.f90 sourcefile~f08estop.f90 f08estop.f90 sourcefile~f08estop.f90->sourcefile~stdlib_error.f90 sourcefile~f18estop.f90 f18estop.f90 sourcefile~f18estop.f90->sourcefile~stdlib_error.f90 sourcefile~stdlib_quadrature_simps.fypp stdlib_quadrature_simps.fypp sourcefile~stdlib_quadrature_simps.fypp->sourcefile~stdlib_error.f90 sourcefile~stdlib_quadrature_trapz.fypp stdlib_quadrature_trapz.fypp sourcefile~stdlib_quadrature_trapz.fypp->sourcefile~stdlib_error.f90 sourcefile~stdlib_stats_distribution_prng.fypp stdlib_stats_distribution_PRNG.fypp sourcefile~stdlib_stats_distribution_prng.fypp->sourcefile~stdlib_error.f90

Contents

Source Code


Source Code

#:include "common.fypp"

#:set KINDS_TYPES = REAL_KINDS_TYPES + INT_KINDS_TYPES + CMPLX_KINDS_TYPES + &
  & [('l1','logical')]

module stdlib_optval
  !!
  !! Provides a generic function `optval`, which can be used to
  !! conveniently implement fallback values for optional arguments
  !! to subprograms
  !! ([Specification](../page/specs/stdlib_optval.html))
  !!
  !! If `x` is an `optional` parameter of a
  !! subprogram, then the expression `optval(x, default)` inside that
  !! subprogram evaluates to `x` if it is present, otherwise `default`.
  !!
  !! It is an error to call `optval` with a single actual argument.
  !!
  use stdlib_kinds, only: sp, dp, qp, int8, int16, int32, int64
  implicit none


  private
  public :: optval


  interface optval
    !! version: experimental
    !!
    !! Fallback value for optional arguments
    !! ([Specification](../page/specs/stdlib_optval.html#description))
    #:for k1, t1 in KINDS_TYPES
      module procedure optval_${t1[0]}$${k1}$
    #:endfor
    module procedure optval_character
     ! TODO: differentiate ascii & ucs char kinds
  end interface optval


contains

  #:for k1, t1 in KINDS_TYPES
    pure elemental function optval_${t1[0]}$${k1}$(x, default) result(y)
    ${t1}$, intent(in), optional :: x
    ${t1}$, intent(in) :: default
    ${t1}$ :: y

    if (present(x)) then
       y = x
    else
       y = default
    end if
  end function optval_${t1[0]}$${k1}$
  #:endfor

  ! Cannot be made elemental
  pure function optval_character(x, default) result(y)
    character(len=*), intent(in), optional :: x
    character(len=*), intent(in) :: default
    character(len=:), allocatable :: y

    if (present(x)) then
       y = x
    else
       y = default
    end if
  end function optval_character

end module stdlib_optval