Skip to main content
Engineering LibreTexts

12.3: Every Class Is an Instance of a Metaclass

  • Page ID
    36398
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    As we mentioned in Section 12.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 by global variables, metaclasses are anonymous. However, we can always refer to them through the class that is their instance. The class of Color, for instance, is Color class, and the class of Object is Object class:

    Color class    →    Color class
    Object class   →    Object class 
    

    Figure \(\PageIndex{1}\) shows how each class is an instance of its (anonymous) metaclass.

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

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

    Color subclasses                         →    {TranslucentColor}
    TranslucentColor subclasses              →    #()
    TranslucentColor allSuperclasses         →    an OrderedCollection(Color Object ProtoObject)
    TranslucentColor instVarNames            →    #('alpha')
    TranslucentColor allInstVarNames         →    #('rgb' 'cachedDepth' 'cachedBitPattern' 'alpha')
    TranslucentColor selectors               →    an IdentitySet(#alpha:
        #asNontranslucentColor #privateAlpha #pixelValueForDepth: #isOpaque
        #isTranslucentColor #storeOn: #pixelWordForDepth: #scaledPixelValue32 #alpha
        #bitPatternForDepth: #hash #convertToCurrentVersion:refStream:
        #isTransparent #isTranslucent #setRgb:alpha: #balancedPatternForDepth:
        #storeArrayValuesOn:)
    

    This page titled 12.3: Every Class Is an Instance of a Metaclass is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz, Damien Pollet via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.