Skip to content

constructing an object of class type X with a metatype value must use a 'required' initializer  #71874

Open
swiftlang/swift-corelibs-foundation
#4884
@Kyle-Ye

Description

@Kyle-Ye

Description

The following code can compile on Darwin platform but on Linux platform we will get a compiler error.

swift build
Building for debugging...
DemoKit.swift:8:16: error: constructing an object of class type 'Delegate' with a metatype value must use a 'required' initializer
        return Delegate()
               ^~~~~~~~
Foundation.NSObject:2:12: note: selected non-required initializer 'init()'
    public init()
           ^
error: fatalError

Reproduction

Run swift package init to add a new empty package. And then update the main file to the following code.

import Foundation

class Box<Delegate: NSObject> {
    var delegate: NSObject {
        return Delegate()
    }
}

Expected behavior

Compile success on Linux.

Environment

swiftc --version
Swift version 5.9.2 (swift-5.9.2-RELEASE)
Target: aarch64-unknown-linux-gnu

uname -a
Linux ubuntu-2204 6.5.13-orbstack-00122-g57b8027e2387 #1 SMP Tue Feb  6 07:48:26 UTC 2024 aarch64 aarch64 aarch64 GNU/Linux

Additional information

Forums discussion: https://forums.swift.org/t/nsobject-related-compile-issue-on-linux/70266/8

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions