inch_size <- sample(c(14, 16, 19, 24, 32), 1)
# 最后一个未命名的参数是默认值
verdict <- switch(
as.character(inch_size),
"14" = "too small",
"16" = "good for the past decade",
"19" = "for office work",
"24" = "great choice",
"unknown size")
# 陷阱:既无匹配又无默认值时得到 NULL
no_default <- switch("40", "14" = "too small")
cat("inch_size =", inch_size, "\n")
cat("verdict =", verdict, "\n")
cat("is.null(no_default) =",
is.null(no_default), "\n")
# is.null(no_default) = TRUE