Skip to content

Commit d724f48

Browse files
committed
Frame function subtypes in terms of error subtypes
1 parent 16a4135 commit d724f48

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

TSPL.docc/ReferenceManual/Declarations.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,11 +1482,6 @@ Rule for inferring the thrown error type
14821482
Xref to the guide <doc:ErrorHandling#Specifying-a-Concrete-Error-Type>
14831483
-->
14841484

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-
14901485
A functions type includes whether it throws,
14911486
and what type of error it throws.
14921487
As a result, you can use a nonthrowing function

TSPL.docc/ReferenceManual/Types.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -347,21 +347,34 @@ that is, a function that takes an `Int` and returns
347347
another function that takes and returns an `Int`.
348348

349349
Function types for functions
350-
that can throw or rethrow an error must be marked with the `throws` keyword.
351-
The `throws` keyword is part of a function's type,
352-
and nonthrowing functions are subtypes of throwing functions.
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.
350+
that can throw or rethrow an error must include the `throws` keyword.
351+
<!-- XXX TR: Confirm rethrowing functions use 'throws' -->
352+
You can include a type after `throws` in parentheses
353+
to specify the type of error that the function throws.
354+
Writing `throws` without specifying an type
355+
is the same as writing `throws(any Error)`.
356+
Omitting `throws` is the same as writing `throws(Never)`.
357+
358+
The type of error that a function throws is part of that function's type,
359+
and a subtype relationship between error types
360+
means the corresponding function types are also subtypes.
361+
For example, if you declare a custom `MyError` type,
362+
the relationship between some function types is as follows,
363+
from supertype to subtype:
364+
365+
1. Functions that throw any error, marked `throws(any Error)`.
366+
1. Functions that throw a specific error, marked `throws(MyError)`.
367+
1. Functions that don't throw, marked `throws(Never)`.
368+
369+
As a result of these subtype relationships:
370+
371+
- You can use a nonthrowing function
372+
in the same places as a throwing function.
373+
- You can use a function that throws a concrete error type
374+
in the same places as a throwing function.
375+
- You can use a function that throws a more specific error type
376+
in the same places as a function that throws a more general error type.
377+
365378
Throwing and rethrowing functions are described in
366379
<doc:Declarations#Throwing-Functions-and-Methods>
367380
and <doc:Declarations#Rethrowing-Functions-and-Methods>.

0 commit comments

Comments
 (0)