Menu
Coddy logo textTech

Default Methods

Part of the Object Oriented Programming section of Coddy's R journey. Lesson 8 of 57.

When no method matches the class, UseMethod() looks for the default method, name.default. It is the fallback for every class the generic does not know:

describe <- function(x) UseMethod("describe")
describe.dog <- function(x) paste("a dog called", x$name)
describe.default <- function(x) "something I do not know"
rex <- structure(list(name = "Rex"), class = "dog")
car <- structure(list(), class = "car")
print(describe(rex))
print(describe(car))

Output:

[1] "a dog called Rex"
[1] "something I do not know"

Without a default method, calling the generic on an unknown class stops with an error:

describe <- function(x) UseMethod("describe")
describe.dog <- function(x) paste("a dog called", x$name)
car <- structure(list(), class = "car")
describe(car)

Output:

Error in UseMethod("describe") : 
  no applicable method for 'describe' applied to an object of class "car"
Calls: describe
Execution halted

Plain values dispatch too. A number without a class attribute has the implicit class numeric, text has character, and TRUE has logical, so methods for those names work on ordinary vectors:

describe <- function(x) UseMethod("describe")
describe.numeric <- function(x) paste("numbers adding up to", sum(x))
describe.character <- function(x) paste("words:", paste(x, collapse = " "))
describe.default <- function(x) "something else"
print(describe(c(1, 2, 3)))
print(describe(c("a", "b")))
print(describe(TRUE))

Output:

[1] "numbers adding up to 6"
[1] "words: a b"
[1] "something else"

A default method can also refuse politely: stop() with a message that names the class, so the caller knows what went wrong. class(x)[1] is the first class of the object:

area <- function(shape) UseMethod("area")
area.square <- function(shape) shape$side^2
area.default <- function(shape) {
  stop("cannot compute the area of a ", class(shape)[1], call. = FALSE)
}
for (s in list(structure(list(side = 2), class = "square"), structure(list(), class = "blob"))) {
  cat(tryCatch(area(s), error = function(e) conditionMessage(e)), "\n")
}

Output:

4 
cannot compute the area of a blob 
challenge icon

Challenge

Easy

Complete the generic describe(x) in Describe.R and its methods for plain vectors:

  • describe.numeric() returns 3 numbers, sum 6.
  • describe.character() returns 2 words, longest: apple (the first of the longest words when there is a tie).
  • describe.logical() returns 2 of 3 are TRUE.
  • describe.default() returns something else.

The supplied code reads lines num 1,2,3, chr apple,kiwi, lgl TRUE,FALSE,TRUE or any other word with values, which it turns into a list. It prints describe() for each.

Your code goes in Describe.R. main.R holds the supplied input/output code and cannot be edited.

Try it yourself

source("Describe.R")

# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
for (line in input) {
  p <- strsplit(line, " ")[[1]]
  values <- strsplit(p[2], ",")[[1]]
  x <- switch(p[1],
    num = as.numeric(values),
    chr = values,
    lgl = as.logical(values),
    list(values))
  cat(describe(x), "\n", sep = "")
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming

Practice on your own: Online R compiler