[swift-evolution] [Proposal draft] Remove objc requirement for optional protocol methods/properties

David Scrève david.screve at dlta-studio.com
Sat Jan 9 16:00:45 CST 2016


## Introduction

When using protocol, we usually need to use optional method or property. We propose to remove the 
requirement of @objc keyword for such protocol.


## Motivation

objc protocol have many restriction to regular Swift protocol : they cannot use Swift s
specific feature. For example, methods cannot use struct, tuple, etc...

Example : 
Considere the following code : 

```swift
struct Person {
    let name:String
    let surname:String
}

protocol PersonRetrievable {
    var personDescription : String { get }
    func loadPerson() -> Person
}

```

If we want to make the loadPerson() method optional, we need to add the attribut @objc 
to the protocol :

```swift
@objc
protocol PersonRetrievable {
    var personDescription : String { get }
    optional func loadPerson() -> Person
}
```

But this code not longer compiles because struct are not supported on objc protocol.

The only workaround is to transform Person struct to class which involve a major change 
because Person is no longer a value type.

## Proposed solution
We propose to remove objc requirement for optional methods and properties in protocol and  
make this kind of code compilable :

```swift
struct Person {
    let name:String
    let surname:String
}

protocol PersonRetrievable {
    var personDescription : String { get }
    optional func loadPerson() -> Person
}

```

## Detailed design
There's no specific design : A non-objc protocol is still not visible in ObjC code.

Type is mandatory for abstract properties since it cannot be inferred.

## Impact on existing code
This change has no impact on existing code, but might change the ABI that is being 
stabilizing in Swift 3.0.

## Alternatives considered
Without this feature, we must use objc objects as parameters.






-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4233 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160109/799b6eda/attachment.p7s>


More information about the swift-evolution mailing list