The proposal also suggests:
extension _ObjectiveCBridgeable {
init?(bridging object: AnyObject)
}
Which would yield:
let foo: AnyObject = NSString(string: "String")
let bar = String(bridging: foo) // “String"
let foo2: AnyObject = NSArray(array: [])
let bar2 = String(bridging: foo) // nil
The code in the proposal works OOB in Swift 2.2 for String/Double/Float, but
not Array and Dictionary. Not sure if it already would work in Swift 3.0 for
that, or there would need to be a little more work done.
> On May 23, 2016, at 8:22 PM, Jordan Rose <[email protected]> wrote:
>
> Let’s see…
>
> let name = plist[“name”].bridge() as String
>
> Not the worst, but definitely better than any of the alternatives below.
> Unfortunately, I think that “if” is out of the question, at least for Swift 3.
>
> Jordan
>
>> On May 23, 2016, at 20:21, Jed Lewison <[email protected]
>> <mailto:[email protected]>> wrote:
>>
>> If we could have extensions on AnyObject, a simple .bridge() would do the
>> trick, right? (Assuming bridge was generic.) I think something along those
>> lines was mentioned in the proposal.
>>
>> Sent from my iPhone
>>
>> On May 23, 2016, at 5:26 PM, Jordan Rose via swift-evolution
>> <[email protected] <mailto:[email protected]>> wrote:
>>
>>> I am way late, but I share Brent’s concerns. I don’t think this addresses
>>> the very common case of “getting a String out of a heterogeneous
>>> dictionary”.
>>>
>>> let name = plist[“name”] as! String
>>>
>>> becomes one of these:
>>>
>>> let name = plist[“name”] as! NSString as String
>>> let name = String(plist[“name”] as! NSString)
>>> let name = String(forceBridging: plist[“name”]) // not in the proposal
>>>
>>> none of which I’m particularly happy with.
>>>
>>> Jordan
>>>
>>>
>>>> On May 19, 2016, at 02:31, Brent Royal-Gordon via swift-evolution
>>>> <[email protected] <mailto:[email protected]>> wrote:
>>>>
>>>>> * What is your evaluation of the proposal?
>>>>
>>>> The review is technically over, but I don't believe a decision has been
>>>> announced yet, so...
>>>>
>>>> I am generally in favor, but I have a serious concern about the
>>>> readability of certain conversions with this change. To wit, conversions
>>>> like these:
>>>>
>>>> myArray as! [NSView]
>>>> myDictionary as! [String: NSView]
>>>>
>>>> Are about to become something more like these:
>>>>
>>>> [NSView](forcedLazyBridging: myArray)
>>>> [String: NSView](forcedLazyBridging: myDictionary)
>>>>
>>>> Or these:
>>>>
>>>> Array<NSView>(forcedLazyBridging: myArray)
>>>> Dictionary<String, NSView>(forcedLazyBridging: myDictionary)
>>>>
>>>> Either option is a significant regression in code readability compared to
>>>> the status quo.
>>>>
>>>> It's enough to make me wonder if we shouldn't have special-cased
>>>> conversion methods for NSArray, NSDictionary, and NSSet:
>>>>
>>>> myArray.of(NSView) // returns [NSView]
>>>> myDictionary.of(NSView, for: String) // returns [String: NSView]
>>>> mySet.of(NSView) // returns
>>>> Set<NSView>
>>>>
>>>> On the other hand, if you *don't* have to specify an element type, these
>>>> aren't so bad:
>>>>
>>>> Array(forcedLazyBridging: myArray)
>>>> Dictionary(forcedLazyBridging: myDictionary)
>>>>
>>>> And it gets even better if you use something a little saner than
>>>> `forcedLazyBridging` for the label.
>>>>
>>>>> * Is the problem being addressed significant enough to warrant a change
>>>>> to Swift?
>>>>
>>>> Yes. Conversions are a mess, and it'll be nice to clean them up.
>>>>
>>>>> * Does this proposal fit well with the feel and direction of Swift?
>>>>
>>>> Yes.
>>>>
>>>>> * If you have used other languages or libraries with a similar feature,
>>>>> how do you feel that this proposal compares to those?
>>>>
>>>> Most languages I've used have had much simpler cast systems with nothing
>>>> particularly close to Swift's bridging casts.
>>>>
>>>>> * How much effort did you put into your review? A glance, a quick
>>>>> reading, or an in-depth study?
>>>>
>>>> Quick reading.
>>>>
>>>> --
>>>> Brent Royal-Gordon
>>>> Architechies
>>>>
>>>> _______________________________________________
>>>> swift-evolution mailing list
>>>> [email protected] <mailto:[email protected]>
>>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>>
>>> _______________________________________________
>>> swift-evolution mailing list
>>> [email protected] <mailto:[email protected]>
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>> <https://lists.swift.org/mailman/listinfo/swift-evolution>
>
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution