This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Unreachable code after return

1 reply
Hans Brattberg
Joined: 2010-07-08,
User offline. Last seen 42 years 45 weeks ago.

Hi!

Maybe this is a newbie question, but is there such a thing as compiler
error for unreachable code in Scala?

For instance this function compiles:

def a() : Unit = {
return
1+1
}

Why?

/Hans

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Unreachable code after return

def foo: Any = {return 1; 2}
^
one warning found
~/code: echo $?
0
~/code: scala -Xfatal-warnings -Ywarn-dead-code -e 'def foo: Any =
{return 1; 2}'
/var/folders/vQ/vQZfxx8pGp8OZLoN+ZWxTk+++TI/-Tmp-/scalacmd2634965528144388727.scala:1:
error: dead code following this construct
def foo: Any = {return 1; 2}
^
one error found
~/code: echo $?1

On Thu, Jul 8, 2010 at 11:45 PM, Hans Brattberg wrote:
> Hi!
>
> Maybe this is a newbie question, but is there such a thing as compiler
> error for unreachable code in Scala?
>
> For instance this function compiles:
>
>        def a() : Unit = {
>                return
>                1+1
>        }
>
> Why?
>
> /Hans
>
> --
> Hans Brattberg
> hans.brattberg@crisp.se
> www.crisp.se/hans.brattberg
> +46 (0)70 575 31 32
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland