ABSTRACT

In chapter 13 we’ll learn about R’s first and simplest OO system: S3. S3 is informal and ad hoc, but there is a certain elegance in its minimalism: you can’t take away any part of it and still have a useful OO system. For these reasons, you should use it, unless you have a compelling reason to do otherwise. S3 is the only OO system used in the base and stats packages, and it’s the most commonly used system in CRAN packages.

“Basics” gives a rapid overview of all the main components of S3: classes, generics, and methods. You’ll also learn about sloop::s3_dispatch(), which we’ll use throughout the chapter to explore how S3 works. Then, “Classes” goes into the details of creating a new S3 class, including the three functions that should accompany most classes: a constructor, a helper, and a validator. Further, “Generics and methods” describes how S3 generics and methods work, including the basics of method dispatch. “Object styles” discusses the four main styles of S3 objects: vector, record, data frame, and scalar. “Inheritance” demonstrates how inheritance works in S3, and shows you what you need to make a class “subclassable”. “Dispatch details” concludes the chapter with a discussion of the finer details of method dispatch including base types, internal generics, group generics, and double dispatch.