Get startedGet started for free

Primitive Generic Functions

Some core functionality of R is defined using primitive functions, which use a special technique for accessing C-code, for performance reasons. Examples of primitive functions include language elements, like if and for, operators like + and $, and mathematical functions like exp and sin. Primitive functions include S3 generics; the complete list of S3 primitive generics can be found using .S3PrimitiveGenerics (docs).

When an S3 generic is primitive, its lookup process to find methods works slightly differently. R will look for methods using the class (docs), as normal, but if nothing is found, the internal C-code function will be called. (Compare this to regular generics, where an error is thrown if no method is found.) This means that if you override the class of an object, fundamental behavior like calculating the length will not be broken.

This exercise is part of the course

Object-Oriented Programming with S3 and R6 in R

View Course

Exercise instructions

A list object, with the class overridden to be "hairstylist" has been assigned in your workspace to the variable hair.

  • View the structure of hair with str() (docs).
  • List all the S3 primitive generics that R knows about.
  • Use the exists() (docs) function to check that no function named length.hairstylist exists. Note that this function takes a character input.
  • Calculate the length() (docs) of the hair variable.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# View the structure of hair
___

# What primitive generics are available?
___

# Does length.hairstylist exist?
___

# What is the length of hair?
___
Edit and Run Code