[swift-evolution] Swift Extensions on Overlay Structs
Fabian Ehrentraud
Fabian.Ehrentraud at willhaben.at
Mon Nov 14 01:43:00 CST 2016
That's an interesting idea without having to write type signatures twice.
The different nullability of the absoluteURL is strange indeed - what if I do a guaranteed cast from NSURL to URL, and absoluteURL would have been nil in the original object?
Eiher way, I'd really like to know if URL extensions could be bridged too without casting in a future Swift version.
-- Fabian
On 13 Nov 2016, at 03:45, Dennis Lysenko <dennis.s.lysenko at gmail.com<mailto:dennis.s.lysenko at gmail.com>> wrote:
Hmm... would it be possible to define a protocol like "UnifiedURLType", define each of the properties you need to work with inside your extension functions as protocol properties inside the protocol UnifiedURLType {} declaration, create an extension UnifiedURLType {} and include doSomething() inside the extension, and then create extensions on both URL and NSURL causing them to conform to UnifiedURLType? (extension URL: UnifiedURLType {}; extension NSURL: UnifiedURLType {})
You could try it, but I wouldn't hold my breath, especially if you're planning on using absoluteURL within your extension as something as basic as the type of that variable is already different between the two types.
Dennis
On Fri, Nov 11, 2016 at 3:08 AM Fabian Ehrentraud via swift-evolution <swift-evolution at swift.org<mailto:swift-evolution at swift.org>> wrote:
Hi list,
Since Swift 3 there exist overlay structs, e.g. NSURL gets bridged to URL.
Unfortunately now extensions on URL are not getting bridged back to ObjC:
extension URL {
func doSomething() -> URL {
return self.absoluteURL
}
}
+ (NSURL *)swiftStructExtensionCaller {
NSURL *url = [NSURL URLWithString:@"https://apple.github.io/swift-evolution/"];
return [url doSomething];
}
The compiler does not see the method `doSomething`. Is this on purpose, or something that has yet to be improved?
I'm aware that I could write the extension on NSURL, but that would mean a lot of casting on the Swift side.
-- Fabian
_______________________________________________
swift-evolution mailing list
swift-evolution at swift.org<mailto: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/20161114/d8146af2/attachment.html>
More information about the swift-evolution
mailing list