The internal reorganization of the object should be transparent to the
user i.e. compact(x) should "look" the same as x, or,
more precisely, x and compact(x) should be interchangeable
anywhere in the user's code. However, because they have different internal
representations, we generally don't expect identical(x, compact(x))
to be TRUE, even though most of the times they will, because there are
only very few types of objects that compact actually knows how to
reorganize internally. compact is a generic function.
Here is how the default method works. By default compact(x) is
obtained by compacting all the "components" in x. Only 2 kinds
of objects are considered to have "components": lists (the components
are the list elements), and S4 objects (the components are the slots).
The other objects are not considered to have components, so, by default,
compact does nothing on them. In particular, it does nothing on
environments. Also the attributes of an object (other than the slots of
an S4 object) are not considered to be "components" and therefore are
not compacted.
Note that, in the absence of specialized compact methods that
actually know how to reorganize an object internally, the default method
would visit the tree of all the components, sub-components,
sub-sub-components etc of object x without actually modifying
anything in x. So of course, specialized compact methods
need to be defined for the objects that can *effectively* be compacted.
Otherwise the compact function would be equivalent to the
identity function!
At the moment, 2 specialized compact methods are defined (in
addition to the default method): one for XVector objects, and
one for XVectorList objects.