[swift-evolution] Proposal: Allow Getters and Setters to Throw

Matthew Johnson matthew at anandabits.com
Sun Dec 6 07:15:12 CST 2015


Great to hear Chris and thanks for mentioning generic subscripts as well.  I have a use case for a generic throwing subscript so I hope patches will be forthcoming.

Sent from my iPad

> On Dec 6, 2015, at 12:07 AM, Chris Lattner <clattner at apple.com> wrote:
> 
> 
>>> On Dec 5, 2015, at 9:08 PM, Nikolai Vazquez via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> One of the aspects of Swift that I like is computed properties for structures and classes. It allows for adding logic when obtaining values or for having the returned value be 
>> On Dec 5, 2015, at 7:17 PM, Matthew Johnson via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> +1 to this.  Subscripts should also be able to throw and currently cannot.
> 
> +1 from me as well.  Subscripts also currently cannot be generic.
> 
> I consider all of these implementation limitations, not intentional parts of the design of Swift.  Patches would be very very welcome to help improve these areas.
> 
> -Chris
> 
> 
>> 
>> dependent on another.
>>> 
>>> As of the `ErrorType` protocol introduction in Swift 2, we can throw errors when it comes to functions and initializers. However, this does not apply to getters and setters.
>>> 
>>> ```swift
>>> struct File<Data> {
>>>     var contents: Data {
>>>         get throws { ... }
>>>         set throws { ... }
>>>     }
>>> }
>>> ```
>>> 
>>> A better example would be getting and setting the current working directory of the process:
>>> 
>>> ```swift
>>> import Foundation
>>> 
>>> extension Process {
>>>     static var workingDirectory: String {
>>>         get {
>>>             let fileManager = NSFileManager.defaultManager()
>>>             return fileManager.currentDirectoryPath
>>>         }
>>>         set throws {
>>>             let fileManager = NSFileManager.defaultManager()
>>>             guard fileManager.changeCurrentDirectoryPath(newValue) else {
>>>                 throw Error("...")
>>>             }
>>>         }
>>>     }
>>> }
>>> 
>>> 
>>> 
>>> ```
>>> 
>>> 
>>>  _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>> 
>>  _______________________________________________
>> 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/20151206/12f9f69b/attachment-0001.html>


More information about the swift-evolution mailing list