15.3: Every Class Is an Instance of a Metaclass

Pharo by Example 5.0 · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-SA 3.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    15.3: Every Class Is an Instance of a Metaclass

    As we mentioned earlier in Section 15.1, classes whose instances are themselves classes are called metaclasses.

    Metaclasses are implicit

    Metaclasses are automatically created when you define a class. We say that they are implicit since as a programmer you never have to worry about them.

    An implicit metaclass is created for each class you create, so each metaclass has only a single instance.

    Whereas ordinary classes are named, metaclasses are anonymous. However, we can always refer to them through the class that is their instance. The class of SortedCollection, for instance, is SortedCollection class, and the class of Object is Object class:

    SortedCollection class
    >>> SortedCollection class
    
    Object class
    >>> Object class
    

    In fact metaclasses are not truly anonymous, their name is deduced from the one of their single instance.

    SortedCollection class name
    >>> 'SortedCollection class'
    

    Figure \(\PageIndex{1}\) shows how each class is an instance of its metaclass. Note that we only skip SequenceableCollection and Collection from the figures and explanation due to space constraints. Their absence does not change the overall meaning.

    The metaclasses of SortedCollection.
    Figure \(\PageIndex{1}\): The metaclasses of SortedCollection and its superclasses (elided).

    Querying Metaclasses

    The fact that classes are also objects makes it easy for us to query them by sending messages. Let’s have a look:

    OrderedCollection subclasses
    >>> {SortedCollection . ObjectFinalizerCollection .
        WeakOrderedCollection . OCLiteralList . GLMMultiValue}
    
    SortedCollection subclasses
    >>> #()
    
    SortedCollection allSuperclasses
    >>> an OrderedCollection(OrderedCollection SequenceableCollection
        Collection Object ProtoObject)
    
    SortedCollection instVarNames
    >>> #('sortBlock')
    
    SortedCollection allInstVarNames
    >>> #('array' 'firstIndex' 'lastIndex' 'sortBlock')
    
    SortedCollection selectors
    >>> #(#indexForInserting: #sort:to: #addAll: #reSort #sortBlock:
        #copyEmpty #addFirst: #insert:before: #defaultSort:to: #median
        #at:put: #add: #= #collect: #flatCollect: #sort: #join: #sortBlock)
    

    This page titled 15.3: Every Class Is an Instance of a Metaclass was last modified on Sat, 19 Dec 2020 08:13:45 GMT and is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by via source content that was edited to the style and standards of the LibreTexts platform.