Big encapsulation-style languages, such as Java, Kotlin, or C#, have a distinction between classes and types. S3, being a generic-function style OOP system, is fundamentally different. But does it still have a class/type distinction of any kind?
Asked
Active
Viewed 143 times
0
-
1S3 is best thought of as a mechanism of method dispatch. It chooses which method to call based on the _class_ of an object. S3 dispatch itself has no notion of a _type_. In R there _is_ the notion of a type (also known as _storage mode_), which can be found by calling `typeof(object)`. This is rarely used directly by the end user. However, for an object that has no "class" attribute, the _type_ is looked up to infer its class, and therefore one could perhaps say that a type is used by S3 as a fallback when there is no specific class attribute. – Allan Cameron Aug 03 '22 at 21:53
-
In R whatever class you will have, it must be of a any of the basic types: `logical/integer/numeric/character/list/symbol/language/expression`. These are the basetypes. Everything else has one of these types. – Onyambu Aug 03 '22 at 21:54
-
1@onyambu there's also objects of S4 type, which has its own SEXP / type / storage mode in the underlying C code. – Allan Cameron Aug 03 '22 at 22:05
-
@AllanCameron And on top of that, I think there's an object type to do with memory... `raw`? – J. Mini Aug 04 '22 at 15:09
-
1There are a few more, they're documented in `?typeof` : Current values are the vector types "logical", "integer", "double", "complex", "character", "raw" and "list", "NULL", "closure" (function), "special" and "builtin" (basic functions and operators), "environment", "S4" (some S4 objects) and others that are unlikely to be seen at user level ("symbol", "pairlist", "promise", "language", "char", "...", "any", "expression", "externalptr", "bytecode" and "weakref"). – moodymudskipper Aug 09 '22 at 14:52
-
Some of these, at least "NULL" and "symbol", cannot have attributes so cannot have a S3 class. – moodymudskipper Aug 09 '22 at 14:54
1 Answers
1
Based on the link attached in the question , in java
there is primitive types and reference types.
The main deference between them is the first holds the value (in the stack memory) but in the second the variable holds (pointer
) address to the value which being created in the (heap memory
) .
In R
the first type (primitive types
) doesn't exist , so every object in R
is just address (pointer
) to the values.
1.1 SEXPs What R users think of as variables or objects are symbols which are bound to a value. The value can be thought of as either a SEXP (
a pointer
), or the structure it points to
So the type of objects in R
is the return of SEXPTYPEs
which are 25 types SEXPTYPEs , But the classes is just attribute
attached to the object Attributes .

Mohamed Desouky
- 4,340
- 2
- 4
- 19
-
2For the purposes of the S3 system, this is a little incomplete, since you can still get the class of any R object even if it has no class attribute. When an object doesn't have a class attribute, its class is inferred from the underlying type. – Allan Cameron Aug 04 '22 at 13:15