Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Allow tail calls after return statements #261

Closed
mbovel opened this issue Nov 23, 2021 · 2 comments
Closed

Allow tail calls after return statements #261

mbovel opened this issue Nov 23, 2021 · 2 comments
Labels
Spree Suitable for a spree

Comments

@mbovel
Copy link
Member

mbovel commented Nov 23, 2021

Minimal example

  @annotation.tailrec
  def sum(n: Int, acc: Int): Int =
    if n != 0 then return sum(n - 1, n + acc)
    acc

fails:

3 |    if n != 0 then return sum(n - 1, n + acc)
  |                          ^^^^^^^^^^^^^^^^^^^
  |                 Cannot rewrite recursive call: it is not in tail position

Rational

Early-returns are useful to avoid nesting conditions. For example, this would be better written with early returns.

Note: this might be a nice issue for a spree.

@som-snytt
Copy link

Related ticket under the rubric "muck with return make return useful again" #256

@mbovel mbovel added the Spree Suitable for a spree label Nov 24, 2021
@DavidGregory084
Copy link

Looks like this was merged in scala/scala3#14067 so perhaps this can be closed?

@mbovel mbovel closed this as completed Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Spree Suitable for a spree
Projects
None yet
Development

No branches or pull requests

3 participants