# NOT RUN {
setIfNull(NULL, "foo")
setIfNull(10, 0)
setIfNull("15", 0)
# This function can be useful for adding elements to a list
testList <- list("a" = 1, "b" = "baz", "c" = "4")
testList$a <- setIfNull(testList$a, 0)
testList$b <- setIfNull(testList$c, 0)
testList$d <- setIfNull(testList$d, 6)
# Be careful about unintended results
setIfNull("T", FALSE) # This returns `TRUE`
setIfNull(12.8, 5L) # This returns the integer 12
# }
# NOT RUN {
# Not all coercions are possible
setIfNull("bar", 5)
setIfNull("5i", 0+0i)
setIfNull("t", FALSE)
# }
Run the code above in your browser using DataLab