Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala return type for tuple-functions

I want to make a scala function which returns a scala tuple.

I can do a function like this:

def foo = (1,"hello","world") 

and this will work fine, but now I want to tell the compiler what I expect to be returned from the function instead of using the built in type inference (after all, I have no idea what a (1,"hello","world") is).

like image 737
Felix Avatar asked Apr 30 '10 10:04

Felix


People also ask

How do I return multiple values from a function in Scala?

You can return multiple values by using tuple. Function does not return multiple values but you can do this with the help of tuple.

Is tuple immutable in Scala?

In Scala, a tuple is a value that contains a fixed number of elements, each with its own type. Tuples are immutable.

Can we have variables of different types inside a tuple Scala?

Thankfully, Scala already has a built-in tuple type, which is an immutable data structure that we can use for holding up to 22 elements with different types.


1 Answers

def foo : (Int, String, String) = (1, "Hello", "World") 

The compiler will interpret the type (Int, String, String) as a Tuple3[Int, String, String]

like image 152
oxbow_lakes Avatar answered Oct 09 '22 03:10

oxbow_lakes