Skip to content

Commit d39a7e0

Browse files
authored
Clarify the restriction on pattern variables in guards. (#2981)
1 parent 3938701 commit d39a7e0

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

accepted/future-releases/0546-patterns/feature-specification.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,6 +2303,17 @@ appears:
23032303
assign to the variable introduced by an initializing formal inside the
23042304
initializer list.*
23052305
2306+
*It's still possible for a user to observe that a guard sees a different
2307+
variable than the body if the guard closes over its variable and the body
2308+
assigns to the body's variable. Preventing that by disallowing capturing a
2309+
pattern variable in a guard would get in the way of common use cases like
2310+
calling `Iterable.where()` or `Iterable.any()` in a guard. Preventing it by
2311+
disallowing assignment in the body would be confusing for a variable that
2312+
isn't declared `final` and might usefully be assigned. Instead, we minimize
2313+
the chances of a user seeing that the guard has its own variable by
2314+
disallowing assignment and accept that in rare cases, a user may still
2315+
observe that the guard sees a different variable.*
2316+
23062317
If the body of a switch statement or expression is reached through only a
23072318
single case, then it is executed in a new scope whose enclosing scope is the
23082319
case scope of that case. Otherwise, the body is executed in a new scope

0 commit comments

Comments
 (0)