Skip to content

Commit

Permalink
Fix throwReturn of NonLocalReturns to allow wider usage
Browse files Browse the repository at this point in the history
  • Loading branch information
soronpo committed Jun 26, 2022
1 parent 75d8eea commit 8f4929a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion library/src/scala/util/control/NonLocalReturns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object NonLocalReturns {
}

/** Performs a nonlocal return by throwing an exception. */
def throwReturn[T](result: T)(using returner: ReturnThrowable[T]): Nothing =
def throwReturn[T](result: T)(using returner: ReturnThrowable[? >: T]): Nothing =
returner.throwReturn(result)

/** Enable nonlocal returns in `op`. */
Expand Down
13 changes: 12 additions & 1 deletion tests/run/nonlocal-return.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ object Test {
false
}

trait Animal
object Dog extends Animal
object Cat extends Animal

def animal(arg: Int): Animal = returning {
if (arg < 0) throwReturn(Dog)
Cat
}

def main(arg: Array[String]): Unit = {
assert(has(1 :: 2 :: Nil, 1))
assert(has(1 :: 2 :: Nil, 2))
assert(!has(1 :: 2 :: Nil, 3))
assert(animal(1) == Cat)
assert(animal(-1) == Dog)
}
}
}

0 comments on commit 8f4929a

Please sign in to comment.