apply() for Optionals
Originator: | robnapier | ||
Number: | rdar://23247992 | Date Originated: | 24-Oct-2015 11:02 AM |
Status: | Open | Resolved: | |
Product: | Developer Tools | Product Version: | 7.1 |
Classification: | Enhancement | Reproducible: | Not Applicable |
SequenceType now has forEach, but there is no equivalent for Optional. While map() works, it is not ideal for Void returning functions for the same reason as in SequenceType. It forces you to assign the result to _ in order to avoid a “result of call is unused” warning, and it is misleading. You do not “map” to side-effects. Optionals should have a method with a name like “apply” that allows them to cause a side effect if they are non-nil. For example: extension Optional { func apply(@noescape f: (Wrapped) throws -> Void) rethrows { try map(f) } } Swift should still encourage the use of if-let, just as it encourages for-in, but in map/filter chains where forEach() makes sense, apply() equally makes sense.
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!