[swift-evolution] Proposal: Bridging for Swift Structs in Objective-C

Chris Lattner clattner at apple.com
Sat Dec 5 11:57:35 CST 2015


> On Dec 4, 2015, at 2:06 PM, Dan Stenmark <daniel.j.stenmark at gmail.com> wrote:
> 
> 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
> 	}
> }

I’m generally a fan of anything that can make Swift get exposed back to Objective-C better, but such things generally need implementation work on Clang as well as on Swift (and thus, need Clang community buy-in as well).  Further, as other folks have pointed out, as proposed, this has some challenges in terms of design and implementation.  Are you planning to lead the implementation effort?

-Chris


> 
> 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
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151205/f641c11a/attachment-0001.html>


More information about the swift-evolution mailing list