return in an anonymous function

139 views
Skip to first unread message

John L. Cheng

unread,
Aug 1, 2011, 1:09:03 AM8/1/11
to scala-user
This appears to be a bug. Any idea what's going on here?

object Weird {
def main(argv: Array[String]): Unit = {
val func: (Any => Unit) = {
case 1 => println("One")
case _ =>
println("Two")
return
}
func(1)
func(2)
func(1)
}
}

$ scala -version
Scala code runner version 2.9.0.1 -- Copyright 2002-2011, LAMP/EPFL
$ scala src/Weird.scala
One
Two

Note that it stops after 'func(2)'

When running in the interpreter

$ scala
Welcome to Scala version 2.9.0.1 (Java HotSpot(TM) 64-Bit Server VM,
Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val func: (Any => Unit) = {
| case 1 => println("One")
| case _ =>
| println("Two")
| return
| }
<console>:11: error: return outside method definition
return



Tony Morris

unread,
Aug 1, 2011, 1:11:59 AM8/1/11
to scala...@googlegroups.com
You're returning from main. Avoid return.
--
Tony Morris
http://tmorris.net/


Debasish Ghosh

unread,
Aug 1, 2011, 1:33:53 AM8/1/11
to John L. Cheng, scala-user
You can do a return from a method, not a function. If u return from a function within a method, it's going to be a non local return .. As Tony says, avoid return ..
--
Debasish Ghosh
http://manning.com/ghosh

Twttr: @debasishg
Blog: http://debasishg.blogspot.com
Code: http://github.com/debasishg

Adam Jorgensen

unread,
Aug 1, 2011, 1:58:24 AM8/1/11
to scala...@googlegroups.com
Also, aren't there some rules about using return in functions with a return type of Unit?

John Cheng

unread,
Aug 1, 2011, 9:27:53 AM8/1/11
to dgh...@acm.org, scala-user
Thanks!I I did some more research after seeing your reply. It looks like my problem was having a return in an anonymous function (From Scala Reference Draft)

Returning from a nested anonymous function is implemented by throwing and
catching a scala.runtime.NonLocalReturnException. Any exception catches between the point of return and the enclosing methods might see the exception. Expressions
key comparison makes sure that these exceptions are only caught by the method
instance which is terminated by the return.

Also found a couple of related items 


It caught me off guard that return in an anonymous function is non-local, but a return from a named function is local.
--
---
John L Cheng

Lex

unread,
Aug 1, 2011, 1:58:44 PM8/1/11
to John Cheng, dgh...@acm.org, scala-user
Should the NonLocalReturnException be Throwable instead to avoid
catch-all blocks?

Alex Cruise

unread,
Aug 1, 2011, 2:28:31 PM8/1/11
to Lex, John Cheng, dgh...@acm.org, scala-user
On Mon, Aug 1, 2011 at 10:58 AM, Lex <lex...@gmail.com> wrote:
Should the NonLocalReturnException be Throwable instead to avoid
catch-all blocks?

class NonLocalReturnControl[T](val key: AnyRef, val value: T) extends ControlThrowable

trait ControlThrowable extends Throwable with NoStackTrace

trait NoStackTrace extends Throwable {
  override def fillInStackTrace(): Throwable = 
    if (sys.SystemProperties.noTraceSupression) super.fillInStackTrace()
    else this
}

// -0xe1a  :)

Trond Olsen

unread,
Aug 1, 2011, 3:56:37 PM8/1/11
to John Cheng, scala-user

Lex

unread,
Aug 2, 2011, 3:05:25 PM8/2/11
to Alex Cruise, John Cheng, dgh...@acm.org, scala-user
A quick google search shows old scala-doc, where NonLocalReturnControl
extends RuntimeException. Good to know it's been fixed.
Reply all
Reply to author
Forward
0 new messages