Skip to content

Compilation error for generic NSObject: Required init #885

Description

@vgritsenko

Commit hash: 0dd772b

Context:
While testing swiftly's support of existing Swift code, noticed that a code using generic NSObject (or NSObject subclass) is not compiling. The same code is compiling just fine in Xcode, is it because Foundation library in Xcode treats NSObject as having a required init?

import Foundation

protocol Test: NSObject /* or an NSObject subclass */ { }

func test<T: Test>() -> T {
    return T()
}

Results in the following compilation error:

.../Sample.swift:21:12: error: constructing an object of class type 'T' with a metatype value must use a 'required' initializer
19 | 
20 | func test<T: Test>() -> T {
21 |     return T()
   |            `- error: constructing an object of class type 'T' with a metatype value must use a 'required' initializer
22 | }
23 | 

Foundation.NSObject.init:2:8: note: selected non-required initializer 'init()'
1 | class NSObject {
2 | public init()}
  |        `- note: selected non-required initializer 'init()'

Environment: Swift 6.3.3 on macOS.

$ swiftly run swift --version
Apple Swift version 6.3.3 (swift-6.3.3-RELEASE)
Target: arm64-apple-macosx26.0

$ uname -a
Darwin it-C01241 25.6.0 Darwin Kernel Version 25.6.0: Sat Jul 11 15:26:21 PDT 2026; root:xnu-12377.161.13~4/RELEASE_ARM64_T6000 arm64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions