Dave's Brain

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()") }

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.