Non-objc protocol inheritance from objc protocol


(Pavel Ivashkov) #1

Hello,

What is the current view on inheriting non-objc protocol from objc
protocol? Is it supported? Is it a valid construct?

import Foundation

@objc protocol Base {}

protocol Some : Base {}

class Nada : NSObject, Some {}

let x: NSObject = Nada()

print("Nada is Some? \(x is Some)")
print("Nada is Base? \(x is Base)")

There are two statements in the docs on @objc attribute:
a) The compiler also implicitly adds the objc attribute to a class that
inherits from another class marked with the objc attribute or a class
defined in Objective-C.
b) Protocols marked with the objc attribute can’t inherit from protocols
that aren’t.

But they don't spec the other way round about protocols.

Currently the output is

swift main.swift
Nada is Some? true
Nada is Base? false

Which is not what I would expect.

Here is extended example: http://stackoverflow.com/q/42023977

swift --version
Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
Target: x86_64-apple-macosx10.9

(same on 3.1-dev)


(Jordan Rose) #2

Hi, Pavel. This is definitely supported, and it is indeed a bug that these are not both producing 'true'. We're tracking this as rdar://problem/24453316 <rdar://problem/24453316>.

Jordan

···

On Feb 5, 2017, at 03:14, Pavel Ivashkov via swift-users <swift-users@swift.org> wrote:

Hello,

What is the current view on inheriting non-objc protocol from objc protocol? Is it supported? Is it a valid construct?

import Foundation

@objc protocol Base {}

protocol Some : Base {}

class Nada : NSObject, Some {}

let x: NSObject = Nada()

print("Nada is Some? \(x is Some)")
print("Nada is Base? \(x is Base)")

There are two statements in the docs on @objc attribute:
a) The compiler also implicitly adds the objc attribute to a class that inherits from another class marked with the objc attribute or a class defined in Objective-C.
b) Protocols marked with the objc attribute can’t inherit from protocols that aren’t.

But they don't spec the other way round about protocols.

Currently the output is

swift main.swift
Nada is Some? true
Nada is Base? false

Which is not what I would expect.

Here is extended example: http://stackoverflow.com/q/42023977

swift --version
Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
Target: x86_64-apple-macosx10.9

(same on 3.1-dev)
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Joe Groff) #3

I also filed this as https://bugs.swift.org/browse/SR-3882 since it seems like a reasonable IRGen starter bug too.

-Joe

···

On Feb 6, 2017, at 3:22 PM, Jordan Rose via swift-users <swift-users@swift.org> wrote:

Hi, Pavel. This is definitely supported, and it is indeed a bug that these are not both producing 'true'. We're tracking this as rdar://problem/24453316 <rdar://problem/24453316>.