Skip to content

Commit 7a10094

Browse files
committed
Add a few more bits from SE-0393.
1 parent 9db2e32 commit 7a10094

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

TSPL.docc/GuidedTour/GuidedTour.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,6 +2357,17 @@ anyCommonElements([1, 2, 3], [3])
23572357
Writing `<T: Equatable>`
23582358
is the same as writing `<T> ... where T: Equatable`.
23592359

2360+
Use `repeat` and `each` to make generic functions
2361+
where the number of arguments can vary.
2362+
2363+
```
2364+
func printEach<each T>(_ t: repeat each T) {
2365+
repeat print(each t)
2366+
}
2367+
2368+
printEach(1, "hello", true)
2369+
```
2370+
23602371
> Beta Software:
23612372
>
23622373
> This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

TSPL.docc/LanguageGuide/Generics.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2007,7 +2007,7 @@ What else can you repeat?
20072007
How do you repeat more than one type?
20082008

20092009
- In the simple case, where only one type repeats,
2010-
this means you write `repeat each T` or similar.
2010+
you write `repeat each T` or similar.
20112011

20122012
- For collections or other generic types,
20132013
the pack expansion can happen inside,
@@ -2035,6 +2035,12 @@ How do you constrain the types in a parameter pack?
20352035
- In the more complex case,
20362036
use `repeat each T ` in a trailing `where` clause.
20372037

2038+
- You must restrict the types that appear in a type-parameter pack;
2039+
conformance requirements don't implicitly propagate.
2040+
For example, given `each T: Hashable` writing `repeat Set<each T>` works,
2041+
but it doesn't work with just `each T`
2042+
because `Set` requires `T` to be hashable but the pack doesn't.
2043+
20382044
How do you access the values of a parameter pack?
20392045

20402046
- Inside the function body, you use `repeat`

TSPL.docc/ReferenceManual/Expressions.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,7 @@ A single expression inside parentheses is a parenthesized expression.
15271527

15281528
XXX OUTLINE:
15291529

1530-
- a `repeat` expression must contain one or more `each` expressions
1530+
- A `repeat` expression must contain one or more `each` expressions
15311531
and has the shape `repeat <#repetition pattern#>`
15321532

15331533
- TODO list of contexts where expansion is supported
@@ -1538,11 +1538,27 @@ XXX OUTLINE:
15381538

15391539
- The *repetition pattern* is repeated once for each type in the pack
15401540

1541-
- all of the `each` expressions must expand packs with the same number of types
1541+
- If an expression includes both the `repeat` operator and
1542+
a `try` or `await` operator,
1543+
the `repeat` operator must appear first.
1544+
(So `repeat try each foo` or `repeat each try foo`)
1545+
1546+
- All of the `each` expressions in a parameter-pack expression
1547+
must expand packs that have the same number of types.
1548+
1549+
- In a function declaration,
1550+
an argument whose type is a parameter-pack expansion type
1551+
must be the last parameter
1552+
or the parameter after it must have a label.
15421553

15431554
- It's valid for a pack expression contain no elements,
15441555
in which case the parameter-pack expansion expression isn't evaluated at all (zero times)
15451556

1557+
<!--
1558+
You can't write ( immediately after `each`
1559+
because `each(` is parsed as a function call.
1560+
-->
1561+
15461562
> Grammar of a pack-expansion expression:
15471563
>
15481564
> *parameter-pack-expression***`each`** *expression*

0 commit comments

Comments
 (0)