[swift-evolution] Proposal: Allow Getters and Setters to Throw
Adrian Kashivskyy
adrian.kashivskyy at me.com
Tue Dec 8 17:13:27 CST 2015
One could associate `throws` with getter and setter, not with the subscript itself, e.g.
> subscript(index: Int) {
> get throws {
> // ...
> }
> set {
> // ...
> }
> }
Pozdrawiam – Regards,
Adrian Kashivskyy
> Wiadomość napisana przez Jordan Rose via swift-evolution <swift-evolution at swift.org> w dniu 07.12.2015, o godz. 20:59:
>
> Thanks for bringing this up, Nikolai. IMHO it does need a formal proposal because property (and subscript) access can be used in more interesting ways than just calling the getter or setter. In particular, how does this interact with inout? Is there a "rethrows" equivalent? What are the rules for overrides? (That last is probably very straightforward, but it ought to be written out explicitly.)
>
> Jordan
>
>
>> On Dec 5, 2015, at 19:08, Nikolai Vazquez via swift-evolution <swift-evolution at swift.org <mailto: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 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 <mailto: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/20151209/62c2b942/attachment.html>
More information about the swift-evolution
mailing list