Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-7601] @objc protocol methods cannot return Self #50143

Closed
nicklockwood opened this issue May 4, 2018 · 6 comments
Closed

[SR-7601] @objc protocol methods cannot return Self #50143

nicklockwood opened this issue May 4, 2018 · 6 comments
Assignees
Labels
compiler The Swift compiler in itself improvement

Comments

@nicklockwood
Copy link
Contributor

Previous ID SR-7601
Radar None
Original Reporter @nicklockwood
Type Improvement
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement
Assignee @theblixguy
Priority Medium

md5: d02438d6b1525db5cf95f2cfc2d82dd3

Issue Description:

In Swift, the following is permitted:

protocol Foo {
    static func makeFoo() -> Self
}

But if the protocol is marked as @objc, it generates the warning "Method cannot be a member of an @objc protocol because its result type cannot be represented in Objective-C"

However, adding Self as a return type to a protocol method doesn't usually treat Self as an associatedType requirement, so it's not clear why a protocol method returning Self can't be represented in Objective-C.

The equivalent Objective-C code is permitted and works as you'd expect:

@protocol Foo
+ (instancetype)makeFoo;
@end

@interface Bar: NSObject<Foo>
@end

@implementation Bar
+ (instancetype)makeFoo { return [[self alloc] init]; }
@end
@belkadan
Copy link
Contributor

belkadan commented May 4, 2018

@DougGregor, @rjmccall, do you know the reason for this?

@rjmccall
Copy link
Member

rjmccall commented May 4, 2018

I don't think there's any good reason for it; it seems to me that we could absolutely expose it as `instancetype`.

@DougGregor
Copy link
Member

I agree; there's no reason for this restriction in an Objective-C protocol.

@Dante-Broggi
Copy link
Contributor

This still happens in the swift-DEVELOPMENT-SNAPSHOT-2018-08-26.

@theblixguy
Copy link
Collaborator

PR: #22006

@theblixguy
Copy link
Collaborator

@Dante-Broggi @nicklockwood: Fixed on master.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself improvement
Projects
None yet
Development

No branches or pull requests

6 participants