new_tiger <- function(age) {
structure(list(age = age), class = "Tiger")
}
# switch has no guards: a chain of if
describe <- function(obj) {
if (is.numeric(obj) && obj > 1e6) {
"Big number"
} else if (is.character(obj) &&
nchar(obj) < 256) {
"Short string"
} else if (inherits(obj, "Tiger") &&
obj$age > 12) {
"Old tiger"
} else {
"Unknown type"
}
}
result <- describe(new_tiger(15))
# result = Old tiger
# The guard fails: the next branch is tried
young <- describe(new_tiger(5))
# young = Unknown type
cat("result =", result, "\n")
cat("young =", young, "\n")