Skip to content

[SR-7783] Cannot subclass NSString due to ExpressibleByStringLiteral initializer #3691

Open
@michelf

Description

@michelf
Previous ID SR-7783
Radar rdar://problem/40636610
Original Reporter @michelf
Type Bug
Additional Detail from JIRA
Votes 1
Component/s Compiler, Foundation
Labels Bug
Assignee None
Priority Medium

md5: 7ca07235bdf43a633e747405b099e091

Issue Description:

Normally, you can't add conformance to `ExpressibleByStringLiteral` in a class extension for a non-final class. But somehow Foundation manages to do it. As a consequence, you can't create a subclass of `NSString` or `NSMutableString` because you can't override the required initializer because it comes from an extension.

If you write this:

class C: NSString {
    override init() { super.init() }
}

You get fix-its telling you to add these:

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
required init?(pasteboardPropertyList propertyList: Any, ofType type: NSPasteboard.PasteboardType) {
    fatalError("init(pasteboardPropertyList:ofType:) has not been implemented")
}
required convenience init(stringLiteral value: StaticString) {
    fatalError("init(stringLiteral:) has not been implemented")
}

But there's no way to get the last one to compile because:

"Overriding declarations in extensions is not supported"

Xcode 9.3 (9E145)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions