Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return first element of the tuple

Suppose I create a function that adds two integers :

def addInt(a : Int, b: Int) : (Int, Int) = {
 | val x = a + b
 | (x,2)
 | }

I'm returning (result, 2) on purpose for the sake of this question.

Now I want to create a variable that returns only x.

val result = addInt(3,4) for example

result would return (7,2) but I only want it to return 7. How can I do this? (Without changing the code of the function obviously).

like image 387
SidiAli Avatar asked Sep 20 '25 08:09

SidiAli


1 Answers

val result = addInt(3,4)._1

And if you wanted the 2:

val the2 = addInt(3,4)._2
like image 56
Levi Ramsey Avatar answered Sep 23 '25 06:09

Levi Ramsey