Skip to content

Spec: Non-local returns are deprecated. #17936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions docs/_docs/reference/dropped-features/nonlocal-returns.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: "Deprecated: Nonlocal Returns"
nightlyOf: https://docs.scala-lang.org/scala3/reference/dropped-features/nonlocal-returns.html
---

Returning from nested anonymous functions has been deprecated, and will produce a warning from version `3.2`.
Returning from nested anonymous functions is deprecated since Scala 3.2.0.

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`.

Expand All @@ -21,5 +21,3 @@ def firstIndex[T](xs: List[T], elem: T): Int =
if x == elem then break(i)
-1
```

Note: compiler produces deprecation error on nonlocal returns only with `-source:future` option.
6 changes: 5 additions & 1 deletion docs/_spec/06-expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,11 @@ The type of a return expression is `scala.Nothing`.
The expression ´e´ may be omitted.
The return expression `return` is type-checked and evaluated as if it were `return ()`.

Returning from the method from within a nested function may be implemented by throwing and catching a `scala.runtime.NonLocalReturnControl`.
### Non-Local Returns (deprecated)

Returning from a method from within a nested function is deprecated.

It is implemented by throwing and catching a `scala.runtime.NonLocalReturnControl`.
Any exception catches between the point of return and the enclosing methods might see and catch that exception.
A key comparison makes sure that this exception is only caught by the method instance which is terminated by the return.

Expand Down
23 changes: 23 additions & 0 deletions docs/_spec/APPLIEDreference/dropped-features/nonlocal-returns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
layout: doc-page
title: "Deprecated: Nonlocal Returns"

nightlyOf: https://docs.scala-lang.org/scala3/reference/dropped-features/nonlocal-returns.html
---

Returning from nested anonymous functions is deprecated since Scala 3.2.0.

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`.

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).

Example:

```scala
import scala.util.boundary, boundary.break
def firstIndex[T](xs: List[T], elem: T): Int =
boundary:
for (x, i) <- xs.zipWithIndex do
if x == elem then break(i)
-1
```
29 changes: 0 additions & 29 deletions docs/_spec/TODOreference/dropped-features/nonlocal-returns.md

This file was deleted.