从函数返回多个值 [英] Return multiple values from function

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

问题描述

有没有一种方法可以像在Go(或某些其他语言)中一样在函数return语句中返回几个值(而不是返回对象)?

Is there a way to return several values in a function return statement (other than returning an object) like we can do in Go (or some other languages)?

例如,在Go中,我们可以执行以下操作:

For example, in Go we can do:

func vals() (int, int) {
    return 3, 7
}

可以在Dart中完成吗?像这样:

Can this be done in Dart? Something like this:

int, String foo() {
    return 42, "foobar";
} 


推荐答案

Dart不支持多个返回值。

Dart doesn't support multiple return values.

您可以返回一个数组,

List foo() {
  return [42, "foobar"];
}

或者如果要输入值,请使用元组类,例如包 https://pub.dartlang.org/packages/tuple 提供。

or if you want the values be typed use a Tuple class like the package https://pub.dartlang.org/packages/tuple provides.

另请参见 返回值或错误的方法。

See also either for a way to return a value or an error.

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

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