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

Jean-Daniel Dupas mailing at xenonium.com
Fri Dec 4 17:54:32 CST 2015


How do you deal with the fact that structs have value semantic, while your generated class have reference semantic ?

> Le 4 déc. 2015 à 23:06, Dan Stenmark <daniel.j.stenmark at gmail.com> a écrit :
> 
> 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
> 
> _______________________________________________
> 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/b49e4e0a/attachment.html>


More information about the swift-evolution mailing list