% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/setIfNull.R
\name{setIfNull}
\alias{setIfNull}
\title{Set a variable to a default value if it is NULL}
\usage{
setIfNull(target, default, enforcedType = NULL)
}
\arguments{
\item{target}{Object to test for \code{NULL}.}

\item{default}{Object to return when \code{target} is \code{NULL}.}

\item{enforcedType}{Optional character string specifying the suffix of an
\verb{as.*()} coercion function to apply to the returned value. For example,
\code{"double"} uses \code{as.double()}, \code{"character"} uses \code{as.character()},
and \code{"Date"} uses \code{as.Date()}.

If \code{NULL} (the default), no coercion is performed.}
}
\value{
The value of \code{target} if it is not \code{NULL}; otherwise \code{default}.

If \code{enforcedType} is specified, the returned value is coerced using the
corresponding \verb{as.*()} function.
}
\description{
Returns \code{default} when \code{target} is \code{NULL}; otherwise returns \code{target}
unchanged.
}
\details{
This is useful for assigning default values to optional arguments while
preserving any user-supplied value exactly as provided.

Optionally, \code{enforcedType} may be used to coerce the returned value to a
specific type. This coercion is applied after the \code{NULL} check and affects
both \code{target} and \code{default}.
}
\examples{
setIfNull(NULL, "foo")
setIfNull(10, 0)
setIfNull("15", 0)

# User-supplied values are returned unchanged
setIfNull("15", 0)
setIfNull("mean", 0)
setIfNull(mean, 0)

# Optional type enforcement
setIfNull("15", 0, enforcedType = "double")
setIfNull(NULL, "15", enforcedType = "integer")

}
