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
Exercise instructions
A list object, with the class overridden to be "hairstylist"
has been assigned in your workspace to the variable hair
.
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?
___