Untyped throws
enum ParseError: Error {
case emptyValue
case illegalFormat(String)
case outOfBounds(String)
}
Slide 10
Slide 10 text
Untyped throws
func parseInt(_ value: String) throws -> Int {
if value.isEmpty { throw ParseError.emptyValue }
guard let number = Int(value) else {
if value.contains(/^[-]?\d+$/) {
throw ParseError.outOfBounds(value)
} else {
throw ParseError.illegalFormat(value)
}
}
return number
}
Slide 11
Slide 11 text
Untyped throws
do {
let number = try parseInt(string)
print(number)
} catch {
print(error) // any Error
}
Slide 12
Slide 12 text
Untyped throws
do {
let number: Int = try parseInt(string)
print(number)
} catch let error as ParseError {
switch error { // ParseError
case .emptyValue: ...
case .illegalFormat(let value): ...
case .outOfBounds(let value): ...
}
}
Slide 13
Slide 13 text
⛔
Errors thrown from here are not handled
because the enclosing catch is not exhaus6ve
Slide 14
Slide 14 text
Untyped throws
do {
let number: Int = try parseInt(string)
print(number)
} catch let error as ParseError {
switch error { // ParseError
case .emptyValue: ...
case .illegalFormat(let value): ...
case .outOfBounds(let value): ...
}
}
Slide 15
Slide 15 text
Untyped throws
do {
let number: Int = try parseInt(string)
print(number)
} catch let error as ParseError {
switch error { // ParseError
case .emptyValue: ...
case .illegalFormat(let value): ...
case .outOfBounds(let value): ...
}
} catch {
preconditionFailure("Never reaches here.")
}
Slide 16
Slide 16 text
Untyped throws
func parseInt(_ value: String) throws -> Int
Slide 17
Slide 17 text
Typed throws
func parseInt(_ value: String) throws(ParseError) ->
Int
Slide 18
Slide 18 text
Untyped throws
do {
let number = try parseInt(string)
print(number)
} catch {
print(error) // any Error
}
Slide 19
Slide 19 text
Typed throws
do {
let number = try parseInt(string)
print(number)
} catch {
switch error { // ParseError
...
}
}
Slide 20
Slide 20 text
Untyped throws
do {
let number: Int = try parseInt(string)
print(number)
} catch let error as ParseError {
switch error { // ParseError
case .emptyValue: ...
case .illegalFormat(let value): ...
case .outOfBounds(let value): ...
}
} catch {
preconditionFailure("Never reaches here.")
}
Slide 21
Slide 21 text
Typed throws
do {
let number: Int = try parseInt(string)
print(number)
} catch {
switch error { // ParseError
case .emptyValue: ...
case .illegalFormat(let value): ...
case .outOfBounds(let value): ...
}
}
Slide 22
Slide 22 text
Typed throws͕΄͔ͬͨ͠ଞͷཧ༝
func foo() throws -> Int { // any Error
let result: Result = ...
switch result {
case .success(let value):
return value
case .failure(let error):
throw error // FooError
}
}
• Result, Task ͱͷมͰΤϥʔͷܕ͕ࣦΘΕΔ
Resist the tempta+on to use typed throws1
Typed throwsΛ͏༠ʹ߅͍ͯͩ͘͠͞
1 h$ps:/
/github.com/swi3lang/swi3-evolu:on/blob/main/proposals/0413-typed-throws.md
Slide 55
Slide 55 text
Even with the introduc/on of typed throws
into Swi5, the exis/ng (untyped) throws
remains the be>er default error-handling
mechanism for most Swi5 code.1
typed throws͕Swi.ʹಋೖ͞Εͨͱͯ͠ɺطଘͷʢuntypedʣ
throwsɺ΄ͱΜͲͷSwi.ίʔυʹ͓͍ͯɺґવͱͯ͠ΑΓྑ͍
σϑΥϧτͷΤϥʔϋϯυϦϯάϝΧχζϜͰ͢ɻ
1 h$ps:/
/github.com/swi3lang/swi3-evolu:on/blob/main/proposals/0413-typed-throws.md
array[i] ͕ൣғ֎ʹͳΔଟ͘ͷέʔείʔυͷޡΓ
// i, j͕ൣғ֎ʹͳͬͨ͜ͱ͕Θ͔࣮ͬͯߦ࣌ʹͰ͖Δ͜ͱͳ͍
func sort(_ array: inout [Int]) {
for i in 0 ..< array.count {
for j in i + 1 ..< array.count {
if array[j] < array[i] {
let t = array[j]
array[j] = array[i]
array[i] = t
}
}
}
}