[SR-238] Support throwing subscripts
| Previous ID | SR-238 |
| Radar | None |
| Original Reporter | anviking (JIRA User) |
| Type | Improvement |
Additional Detail from JIRA
| Votes | 24 |
| Component/s | |
| Labels | Improvement, LanguageFeatureRequest |
| Assignee | @theblixguy |
| Priority | Medium |
md5: d1f830bfd05395ee30fb2819cb12b391
relates to:
- SR-115 Support generic constraints in subscripts
Issue Description:
Support for throwing subscripts would together with generic ones be really nice for JSON mapping/deserializing libraries like mine https://github.com/Anviking/Decodable. It would make subscripts an alternative to custom operators.
For example:
subscript<T: SomeProtocol>(key: String) throws -> T
Would most likely be useful on its own for a lot of other things as well.
There is now a WIP implementation that supports throwable getters and setters which can be used inside both computed properties and subscripts. There's still a bit of work left, but hopefully it should be finished soon: https://github.com/apple/swift/pull/22749
Throwing subscripts are available in Swift 5.5 due to SE-0310 Effectful Read-only Properties. Should this issue be marked as resolved?
SE-0310 only allows throwing getter-only subscripts, setters are prohibited when getter is marked as throwing. I believe having a throwing subscript with both getter and setter would be handy:
let element = try array[at: index]
try array[at: index] = value