Dave's Brain

Browse - Programming Tips - How can a method return two things?

Date: 2012nov30
Language: Scala

Q.  How can a method return two things?

A.  You can use the Tuple2 class for that.  Example:

def functionThatReturnsTwoThings(): Tuple2[String,String] = { ("one", "two") // Or more verbose: // return new Tuple2("one", "two") } def test() { val result = functionThatReturnsTwoThings() val one = result._1 val two = result._2 }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, 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.