Suppose I have a scala function that is supposed to return a tuple of type
(String, Int, Int) mapped to keys (words, row, col)
:
def getResult(param: Int) {
// a lot of logic goes here to generate tuple values
// now return the tuple
}
In caller code:
var words, row, col
if(someValue) {
getResults(someValue) // assigns the tuple values to keys words, row and col
// reference words, row and col variables here
} else
getResults(someOtherValue) // assigns the tuple values to keys words, row and col
// reference words, row and col variables here
}
// in this scope here words, row and col are defined and must have values that got assigned in above code
// do something more with words, row and col variables.
The code is incomplete. So how would I declare and return the tuple in the function and how would I use in above scenario?
Is tuple recommended way in above case even though map seems better fit?
Despite all the answers I got, no answer has addressed the issue of how do I declare tuple and fill the tuple later in the code ( not assigning values to tuple at the time declaration like this answer suggests:
var (words, row, col) = if(someValue) {
getResults(someValue)
} else {
getResults(someOtherValue)
}
This is the part of the code that remains unanswered:
var words, row, col // how do I delcare tuple here?
if(someValue) {
getResults(someValue) // assigns the tuple values to keys words, row and col
// how I do that here ?
// reference words, row and col variables here
} else
getResults(someOtherValue) // assigns the tuple values to keys words, row and col
// reference words, row and col variables here
}
In Scala, a tuple is a value that contains a fixed number of elements, each with its own type. Tuples are immutable. Tuples are especially handy for returning multiple values from a method.
You can return multiple values by using tuple. Function does not return multiple values but you can do this with the help of tuple.
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.
Multiple assigment can be performed in a syntactically simple fashion (which relies on the Tuple types' unapply
methods), as follows:
val (foo, bar) = ("Hello", "world")
// The above is entirely equivalent to:
// val foo = "Hello"
// val bar = "world"
//
// or:
//
// val tmp = ("Hello", "world")
// val foo = tmp._1
// val bar = tmp._2
Hence you can simply change the latter example to:
var (words, row, col) = if(someValue) {
getResults(someValue)
} else {
getResults(someOtherValue)
}
The if
statement will return a (String, Int, Int)
and the relevant components will be assigned to words
, row
and col
in order.
If you were asking about how to annotate your method declaration with the return type, that's easy too:
def getResult(param: Int): (String, Int, Int) = {
...
}
As for whether it's better as a map - that depends entirely on the semantics of your method. Are you returning several values at once (a tuple) or are you returning an association between keys and values (a map)? Whichever one feels most natural and convenient is the one that you should use (at least in the absence of other concerns).
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