2

Why does method1 return 1 while method2 2:

def method1 = try { 1 } finally { 2 }                  

def method2: Int = try { return 1 } finally { return 2 }

and why does method2 require specifying its return type?

Alan Coromano
  • 24,958
  • 53
  • 135
  • 205
  • This looks like it is covered here: http://stackoverflow.com/questions/8443743/return-and-try-catch-finally-block-evaluation-in-scala – yakshaver Sep 05 '13 at 05:13
  • Also check http://stackoverflow.com/questions/13892985/return-value-of-finally-block-in-scala – Bask.ws Sep 05 '13 at 05:27

1 Answers1

3

This question will probably be closed by morning, but just for fun:

scala> def foo = return 1
<console>:7: error: method foo has return statement; needs result type
       def foo = return 1
                 ^

SLS 6.20: "The type of a return expression is scala.Nothing."

That answers the second question. It doesn't assist result type inference.

som-snytt
  • 39,429
  • 2
  • 47
  • 129