Browse - Programming Tips - What does a "return" in a "match" do?

Date: 2013jan4
Language: Scala

Q.  What does a "return" in a "match" do?

A.  This surprised me.  I thought => was a function call so
a return would just end the function and control would continue
after the match.  But a return in a match exits the containing function - which is called "test()" in this example:

def test() { var a = "hello" System.out.println("start of test()") a match { case "one" => System.out.println("got one") case "two" => System.out.println("got two") case "hello" => { System.out.println("got hello"); return; } case _ => System.out.println("got default") } System.out.println("end of test()") }