Adam Jorgensen | 1 Aug 2011 07:58
Picon

Re: return in an anonymous function

Also, aren't there some rules about using return in functions with a return type of Unit?

On 1 August 2011 07:33, Debasish Ghosh <ghosh.debasish-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
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 ..


On Mon, Aug 1, 2011 at 10:39 AM, John L. Cheng <johnlicheng-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
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






--
Debasish Ghosh
http://manning.com/ghosh

Twttr: <at> debasishg
Blog: http://debasishg.blogspot.com
Code: http://github.com/debasishg


Gmane