[swift-evolution] Proposal: Bridging for Swift Structs in Objective-C
Dan Stenmark
daniel.j.stenmark at gmail.com
Fri Dec 4 16:06:17 CST 2015
Swift’s Struct type is one of the most praised features of the language, but is currently unavailable in Objective-C. This poses problems for large legacy codebases that can’t be ported to Swift as quickly but still want to begin using some of the mutability semantics it introduces. As such, I’d like to propose syntax for creating bridged classes that can utilized in Objective-C.
@objc struct Letter {
var address : String?
init(withAddress address : String? ) {
self.address = address
}
}
At compile-time, this would create two bridge classes: Letter and MutableLetter, both conforming to the NSMutableCopying protocol.
Letter *letter = [[Letter alloc] initWithAddress:address]; // equivalent to ‘let letter = Letter(address: address)’
MutableLetter *mutableLetter = letter.mutableCopy; // equivalent to ‘var mutableLetter = letter’
With Objective-C’s lack of namespacing, the @objc decorator would likely need to accept an optional class prefix parameter to help guard against class name collisions.
@objc(US) struct Letter {
var address : String?
init(withAddress address : String? ) {
self.address = address
}
}
-------
USLetter *letter = [[USLetter alloc] initWithAddress:address]; // equivalent to ‘let letter = Letter(address: address)’
USMutableLetter *mutableLetter = letter.mutableCopy; // equivalent to ‘var mutableLetter = letter’
Nested types would also be represented via this proposal:
@objc(US) class PostalService {
struct Letter {
var address : String?
init(withAddress address : String? ) {
self.address = address
}
}
}
-------
USPostalServiceLetter *letter = [[USPostalServiceLetter alloc] initWithAddress:address]; // equivalent to ‘let letter = PostalService.Letter(address: address)’
USMutablePostalServiceLetter *mutableLetter = letter.mutableCopy; // equivalent to ‘var mutableLetter = letter’
Dan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151204/2c2e488b/attachment.html>
More information about the swift-evolution
mailing list