[swift-evolution] [Idea] Add forced conversion for Error catching pattern matching
Tyler Fleming Cloutier
cloutiertyler at aol.com
Tue Mar 22 01:22:49 CDT 2016
This is actually quite a good solution. It wouldn’t be quite as consistent if I had multiple catch statements for different types before this but it really gets almost all of the way there. Thanks!
> On Mar 21, 2016, at 6:20 PM, Michel Fortin <michel.fortin at michelf.ca> wrote:
>
> Le 20 mars 2016 à 16:26, Tyler Fleming Cloutier via swift-evolution <swift-evolution at swift.org> a écrit :
>>
>> Would it be wise to allow force conversion for the cases in which the developer believes the match is exhaustive? ie
>>
>> do {
>> let action = chooseAction(game)
>> game = try game.applyAction(action)
>> } catch let e as ActionError {
>> game.failedAction = e
>> } catch _ {
>> fatalError(“This is an unfortunate bit of noise :/")
>> }
>>
>> becomes
>>
>> do {
>> let action = chooseAction(game)
>> game = try game.applyAction(action)
>> } catch let e as! ActionError {
>> game.failedAction = e
>> }
>
> If you move the cast on the next line it'll compile and do what you want with exactly the same amount of code:
>
> do {
> let action = chooseAction(game)
> game = try game.applyAction(action)
> } catch let e {
> game.failedAction = e as! ActionError
> }
>
>
> --
> Michel Fortin
> https://michelf.ca
>
More information about the swift-evolution
mailing list