Can anyone explain why the method below is compiling without any error message? I expected a dead code warning or something like that. Is there a reason why it compiles?
def somethingAfterReturn(): Int = {
println("That is ok..");
return 1
println("WTF is going on here?");
3
}
First, try this:
$ scala -e 'def somethingAfterReturn(): Int = { println("That is ok.."); return 1; println("WTF is going on here?"); 3 }'
no errors, right?
Then, try this:
$ scala -Ywarn-dead-code -e 'def somethingAfterReturn(): Int = { println("That is ok.."); return 1; println("WTF is going on here?"); 3 }'
By default, Scala ignore unreachable code.
The compiler allows this because it is syntactically valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With