swift icon indicating copy to clipboard operation
swift copied to clipboard

[SR-238] Support throwing subscripts

Open swift-ci opened this issue 10 years ago • 3 comments

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.

swift-ci avatar Dec 15 '15 17:12 swift-ci

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

theblixguy avatar Apr 11 '19 01:04 theblixguy

Throwing subscripts are available in Swift 5.5 due to SE-0310 Effectful Read-only Properties. Should this issue be marked as resolved?

xAlien95 avatar Jul 07 '21 00:07 xAlien95

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

endeavour42 avatar Apr 10 '24 15:04 endeavour42