返回元组的第一个元素 [英] Return first element of the tuple

查看:116
本文介绍了返回元组的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我创建一个将两个整数相加的函数:

Suppose I create a function that adds two integers :

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

出于这个问题,我特意返回(result, 2).

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

现在,我想创建一个仅返回x的变量.

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

val result = addInt(3,4) for example

结果将返回(7,2),但我只希望它返回7.我该怎么做? (显然无需更改功能代码).

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).

推荐答案

val result = addInt(3,4)._1

如果您想要2:

val the2 = addInt(3,4)._2

这篇关于返回元组的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆