skip to content

In Swift, how is a method declared in a class extension dispatched, and why can't a subclass override it unless it is `@objc`?

level: middleimportance: should knowfreq 30%

answer

  1. where the vtable gets its slots
  2. an extension can live anywhere
  3. effectively final
  4. the Objective-C method list is open

basics

~20 s

A method declared in a class extension gets no vtable slot, so Swift calls it directly, as if final, and rejects an override. Marked @objc on Apple platforms, it is dispatched by Objective-C message instead, which an override can replace.

solid answer

~50 s

A Swift class's **vtable** holds the overridable members declared in the class body. An extension can be written in any file, or in another module, so Swift does not add its members to the vtable: a call to an extension method is a **direct call**, exactly as if the method were `final`. Because there is no slot for a subclass to replace, the compiler rejects an override with "instance method 'prepare()' is declared in extension of 'Renderer' and cannot be overridden", in Swift 5 and Swift 6 mode alike. Marking the extension method `@objc`, which needs the Objective-C runtime of Apple platforms and an Objective-C-representable signature, changes the mechanism: Swift then calls it with an **Objective-C message**, and a subclass override, which is `@objc` implicitly, is found at run time. In pure Swift code the fix is to move the method into the class body.

code

swift · 9 lines
swift
class Renderer {}

extension Renderer {
  func prepare() -> String { "base" }
}

class MetalRenderer: Renderer {
  override func prepare() -> String { "metal" }
}

go deeper

for a junior

Recall that methods written in a class extension cannot be overridden by a subclass, and that moving them into the class body fixes that.

for a middle

Explain that extension members get no vtable slot, so calls are direct, and that @objc switches them to Objective-C messaging, which an override can replace.

for a senior

Spot the pattern in review when a helper in an extension needs customizing, choose between moving it to the body and @objc, and know @objc costs inlining and is unavailable off Apple platforms.

for a principal

Set a code-organization rule that keeps overridable members in the class body and treats extensions as final helpers, so subclass customization points stay deliberate.

## Why extension members get no vtable slot Swift dispatches overridable class members through a **vtable**: a table of function pointers, one slot per member, laid out when the class is compiled. A subclass copies the table and replaces the slots it overrides. An **extension** adds members to a type after its declaration, and it can be written in any file of the module or even in a different module. Swift does not grow the vtable for those members. A method declared in an extension of a class therefore has no slot, and the compiler calls it **directly**: in the compiler's intermediate language the call is a `function_ref`, never a `class_method`, even in an unoptimized build. In effect, every native (non-`@objc`) extension method of a Swift class behaves like a `final` method. ## What the compiler reports Because there is no slot for a subclass to fill, an override is an error. With the Swift 6.4 compiler, in Swift 5 and Swift 6 language mode alike: - Overriding a method that the superclass declares in an extension gives "instance method 'prepare()' is declared in extension of 'Renderer' and cannot be overridden". When the base class inherits from `NSObject`, the message begins "non-'@objc' instance method", pointing at the way out. - The reverse also fails: overriding a superclass method from an extension of the subclass gives "instance method 'prepare()' declared in 'Renderer' cannot be overridden from extension". An override must fill a vtable slot, so it belongs in the subclass's body. On an `NSObject` hierarchy, even an `@objc` superclass method cannot be overridden from an extension unless it is `@objc dynamic`: the compiler reports "cannot override a non-dynamic class declaration from an extension". ## How `@objc` changes the mechanism On platforms with the Objective-C runtime, an extension member marked `@objc` is registered in the class's **Objective-C method list** under a selector. Swift then calls it with an Objective-C message send (`objc_method` in SIL, `objc_msgSend` at run time) rather than a direct call. The method list is looked up at run time, so a subclass's override is found there: 1. The superclass extension declares `@objc func prepare() -> String`. 2. The subclass writes `override func prepare() -> String` in its body; overriding an `@objc` method makes the override `@objc` too. 3. A call through a `Renderer` reference sends the `prepare` message, and the runtime finds the subclass's implementation. This works only where Objective-C interop exists, so not in Swift on Linux, Windows or WebAssembly. The method's signature must be representable in Objective-C, and the file must import Foundation for `@objc` to be accepted. ## Comparing the options | Where the method is declared | Dispatch from Swift | Can a subclass override it? | |---|---|---| | class body | vtable, unless devirtualized | yes | | class body, `final` | direct | no | | class extension | direct | no, compile-time error | | class extension, `@objc` | Objective-C message send | yes, on Apple platforms | ## Choosing a fix - In pure Swift, **move the method into the class body**. It gets a vtable slot, subclasses can override it, and the optimizer can still devirtualize calls when it proves no override exists. - Use `@objc` in an extension only when the class already lives in the Objective-C world (an `NSObject` subclass) and you need runtime features such as selectors; accept that each call is a message send the compiler does not inline. - If the method should never be overridden, an extension is a fine home: it is direct and cheap, and you can also write `final` there to say so explicitly. Organizing a class's code into extensions is common style, so this rule surprises people when a subclass needs to customize a helper that happens to sit in an extension. The cause is the dispatch mechanism, not the extension syntax. ## Why the call is cheap as a result The same rule that blocks overriding makes extension methods fast. A direct call needs no table lookup, and the optimizer may inline the body into the caller. That is also why moving a method from an extension into the class body can, in rare hot paths, make it slower: it gains a vtable slot, and calls stay dynamic until the compiler proves no override exists, through `final`, `private` or whole-module optimization. Conversely, an `@objc` extension method is the least optimizable option: with the Swift 6.4 compiler it stays an Objective-C message send even in an optimized whole-module build, so its body is not inlined. When reviewing a class split across extensions, ask of each method whether a subclass is ever meant to replace it; the answer decides both where it is declared and how it is dispatched.

  • Is a call to a Swift class's extension method ever a vtable call?
    Not for a native Swift method. Members declared in a class extension have no vtable slot, so Swift calls them directly even in an unoptimized build. The exception is an `@objc` extension member on Apple platforms, which Swift calls with an Objective-C message send instead, the mechanism that makes overriding it possible.
  • Can a Swift subclass override an inherited method from inside its own extension?
    Not for a native Swift method. The compiler reports that the method "declared in 'Renderer' cannot be overridden from extension". An override fills the superclass's vtable slot, so it must be declared in the subclass's body. Only an `@objc dynamic` member can be overridden from an extension; with `@objc` alone the compiler reports "cannot override a non-dynamic class declaration from an extension".

  • Extension methods are added to the class's vtable like any other method
  • The override error in an extension is only a Swift 6 mode restriction
  • Methods of an NSObject subclass are all @objc, so extensions can always be overridden
  • Marking the extension method dynamic without @objc makes it overridable
  • Extension methods are dispatched through the class's witness table