Any reason of "@objc is not supported within extensions of generic classes." ?


(Zhao Xin) #1

I was trying to implement a final class to conform NSCopying by extension.
List<T> was a class in Realm object database.

final class List<T>

My implementation:

extension List:NSCopying {
    public func copy(with zone: NSZone? = nil) -> Any {
        ...
    }
}

I got an error called "@objc is not supported within extensions of generic
classes.". Any idea why there is a limitation of this? Thanks.

Zhaoxin


(Jordan Rose) #2

The way @objc methods are added in an extension is by using Objective-C categories. With a generic class, though, there's no Class object to attach the category metadata to—it's all created dynamically at runtime.

We could potentially support this some day, but it would take some thought, and we'd have to be careful not to drastically slow down the cost of instantiating new concrete types from a heavily-extended generic class.

Jordan

···

On Aug 24, 2016, at 7:23, Zhao Xin via swift-users <swift-users@swift.org> wrote:

I was trying to implement a final class to conform NSCopying by extension. List<T> was a class in Realm object database.

final class List<T>

My implementation:

extension List:NSCopying {
    public func copy(with zone: NSZone? = nil) -> Any {
        ...
    }
}

I got an error called "@objc is not supported within extensions of generic classes.". Any idea why there is a limitation of this? Thanks.

Zhaoxin
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Zhao Xin) #3

Thank you, Jordan.

Zhaoxin

···

On Thu, Aug 25, 2016 at 1:15 AM, Jordan Rose <jordan_rose@apple.com> wrote:

The way @objc methods are added in an extension is by using Objective-C
categories. With a generic class, though, there's no Class object to attach
the category metadata to—it's all created dynamically at runtime.

We could potentially support this some day, but it would take some
thought, and we'd have to be careful not to drastically slow down the cost
of instantiating new concrete types from a heavily-extended generic class.

Jordan

On Aug 24, 2016, at 7:23, Zhao Xin via swift-users <swift-users@swift.org> > wrote:

I was trying to implement a final class to conform NSCopying by extension.
List<T> was a class in Realm object database.

final class List<T>

My implementation:

extension List:NSCopying {
    public func copy(with zone: NSZone? = nil) -> Any {
        ...
    }
}

I got an error called "@objc is not supported within extensions of generic
classes.". Any idea why there is a limitation of this? Thanks.

Zhaoxin
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users