foo <- function(
x = int(1L),
y = chr("Hello!"),
z = lgl(TRUE),
a = dbl(1.1),
b = tbl(mtcars),
c = NULL # This argument will not be checked
) {
# Simply place enforce_types() in your function header!
dataclass::enforce_types()
# Function logic ...
}
# This run the function with the type defaults
foo()
# This will check types but for new arguments
foo(2L, "Hi!", FALSE, 1.2, mtcars)
# This would fail because types are incorrect!
# foo(1.1, FALSE, NULL, "Hi", list())
# This function will only warn when there are type failures
bar <- function(x = int(1)) {
dataclass::enforce_types("warn")
}
Run the code above in your browser using DataLab