Skip to content

Commit 16a4135

Browse files
committed
Describe subtyping rules for throwing functions
1 parent 29cadb2 commit 16a4135

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

TSPL.docc/ReferenceManual/Declarations.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,25 +1465,33 @@ func <#function name#>(<#parameters#>) throws -> <#return type#> {
14651465
}
14661466
```
14671467

1468+
A function that declares what error type it throws
1469+
has the following form:
1470+
1471+
```swift
1472+
func <#function name#>(<#parameters#>) throws(<#error type#>) -> <#return type#> {
1473+
<#statements#>
1474+
}
1475+
```
1476+
14681477
Calls to a throwing function or method must be wrapped in a `try` or `try!` expression
14691478
(that is, in the scope of a `try` or `try!` operator).
14701479

14711480
<!-- XXX
1472-
spellings:
1473-
- throws is the same as throws(any Error)
1474-
- throws(Never) is the same as not-throwing
1475-
14761481
Rule for inferring the thrown error type
14771482
Xref to the guide <doc:ErrorHandling#Specifying-a-Concrete-Error-Type>
14781483
-->
14791484

1480-
The `throws` keyword is part of a function's type,
1481-
and nonthrowing functions are subtypes of throwing functions.
1482-
<!-- XXX
1483-
Subtyping rule for throws(any Error) vs throws(MyErrorType)
1484-
-->
1485+
Writing `throws` without specifying the error type that the function throws
1486+
is the same as writing `throws(any Error)`.
1487+
Writing `throws(Never)` is the same as omitting throws;
1488+
it declares a nonthrowing function.
1489+
1490+
A functions type includes whether it throws,
1491+
and what type of error it throws.
14851492
As a result, you can use a nonthrowing function
14861493
in a context where a throwing one is expected.
1494+
For more information, see <doc:Types#Throwing-Functions>.
14871495

14881496
You can't overload a function based only on whether the function can throw an error.
14891497
That said,

TSPL.docc/ReferenceManual/Types.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -350,15 +350,18 @@ Function types for functions
350350
that can throw or rethrow an error must be marked with the `throws` keyword.
351351
The `throws` keyword is part of a function's type,
352352
and nonthrowing functions are subtypes of throwing functions.
353-
<!-- XXX
354-
A function that throws a concrete error
355-
is a subtype of one that throws 'any Error'.
356-
357-
TR:
358-
If EE is a subtype of E,
359-
is a function that throws EE a subtype of one that throws E?
360-
-->
361-
As a result, you can use a nonthrowing function in the same places as a throwing one.
353+
A function that throws a concrete error type
354+
includes the error type that it throws in its function type.
355+
If that error type is a subtype,
356+
then functions that throw the subtype as an error
357+
are subtypes of functions that throw the supertype as an error.
358+
359+
As a result, you can use a nonthrowing function
360+
in the same places as a throwing function,
361+
you can use a function that throws a concrete error type
362+
in the same places as a throwing function,
363+
and you can use a function that throws a more specific concrete error type
364+
in the same places as a function that throws a more general error type.
362365
Throwing and rethrowing functions are described in
363366
<doc:Declarations#Throwing-Functions-and-Methods>
364367
and <doc:Declarations#Rethrowing-Functions-and-Methods>.

0 commit comments

Comments
 (0)