Skip to content

Commit c7a6328

Browse files
committed
Spec: Non-local returns are deprecated.
1 parent 482dfeb commit c7a6328

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

docs/_docs/reference/dropped-features/nonlocal-returns.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: "Deprecated: Nonlocal Returns"
55
nightlyOf: https://docs.scala-lang.org/scala3/reference/dropped-features/nonlocal-returns.html
66
---
77

8-
Returning from nested anonymous functions has been deprecated, and will produce a warning from version `3.2`.
8+
Returning from nested anonymous functions is deprecated.
99

1010
Nonlocal returns are implemented by throwing and catching `scala.runtime.NonLocalReturnException`-s. This is rarely what is intended by the programmer. It can be problematic because of the hidden performance cost of throwing and catching exceptions. Furthermore, it is a leaky implementation: a catch-all exception handler can intercept a `NonLocalReturnException`.
1111

@@ -21,5 +21,3 @@ def firstIndex[T](xs: List[T], elem: T): Int =
2121
if x == elem then break(i)
2222
-1
2323
```
24-
25-
Note: compiler produces deprecation error on nonlocal returns only with `-source:future` option.

docs/_spec/06-expressions.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,11 @@ The type of a return expression is `scala.Nothing`.
841841
The expression ´e´ may be omitted.
842842
The return expression `return` is type-checked and evaluated as if it were `return ()`.
843843

844-
Returning from the method from within a nested function may be implemented by throwing and catching a `scala.runtime.NonLocalReturnControl`.
844+
### Non-Local Returns (deprecated)
845+
846+
Returning from a method from within a nested function is deprecated.
847+
848+
It is implemented by throwing and catching a `scala.runtime.NonLocalReturnControl`.
845849
Any exception catches between the point of return and the enclosing methods might see and catch that exception.
846850
A key comparison makes sure that this exception is only caught by the method instance which is terminated by the return.
847851

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
layout: doc-page
3+
title: "Deprecated: Nonlocal Returns"
4+
5+
nightlyOf: https://docs.scala-lang.org/scala3/reference/dropped-features/nonlocal-returns.html
6+
---
7+
8+
Returning from nested anonymous functions is deprecated.
9+
10+
Nonlocal returns are implemented by throwing and catching `scala.runtime.NonLocalReturnException`-s. This is rarely what is intended by the programmer. It can be problematic because of the hidden performance cost of throwing and catching exceptions. Furthermore, it is a leaky implementation: a catch-all exception handler can intercept a `NonLocalReturnException`.
11+
12+
A better alternative to nonlocal returns and also the `scala.util.control.Breaks` API is provided by [`scala.util.boundary` and `boundary.break`](http://dotty.epfl.ch/api/scala/util/boundary$.html).
13+
14+
Example:
15+
16+
```scala
17+
import scala.util.boundary, boundary.break
18+
def firstIndex[T](xs: List[T], elem: T): Int =
19+
boundary:
20+
for (x, i) <- xs.zipWithIndex do
21+
if x == elem then break(i)
22+
-1
23+
```

docs/_spec/TODOreference/dropped-features/nonlocal-returns.md

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)