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).
You can return multiple values by using tuple. Function does not return multiple values but you can do this with the help of tuple.
In Scala, a tuple is a value that contains a fixed number of elements, each with its own type. Tuples are immutable.
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.
def foo : (Int, String, String) = (1, "Hello", "World")
The compiler will interpret the type (Int, String, String)
as a Tuple3[Int, String, String]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With