使用C# 7.0推出的值元组和解构功能。
| static (int, int) Calc(int a, int b) |
| { |
| return (a + b, a - b); |
| } |
| static void Main() |
| { |
| var (add, sub) = Calc(8, 2); |
| Console.WriteLine($"{add}, {sub}"); |
| } |
Scala
使用元组。
| def calc(a: Int, b: Int) : (Int, Int) = { |
| (a + b, a - b) |
| } |
| val (add, sub) = calc(8, 2) |
| println(s"$add, $sub") |
Kotlin
使用Pair和解构声明。
| fun calc(a: Int, b: Int): Pair<Int,Int> { |
| return Pair(a + b, a - b) |
| } |
| fun main() { |
| val (add, sub) = calc(8, 2) |
| println("$add, $sub") |
| } |
使用数组和解构声明。
| fun calc(a: Int, b: Int) : Array<Int> { |
| return arrayOf(a + b, a - b) |
| } |
| fun main() { |
| val (add, sub) = calc(8, 2) |
| println("$add, $sub") |
| } |
JavaScript
使用解构赋值。
| function calc(a, b) { |
| return [a + b, a - b]; |
| } |
| let [add, sub] = calc(8, 2); |
| console.log(`${add}, ${sub}`); |
Python
使用元组和解构。
| def calc(a, b): |
| return a + b, a - b |
| |
| |
| add, sub = calc(8, 2) |
| print(f"{add}, {sub}") |
使用列表和解构。
| def calc(a, b): |
| return [a + b, a - b] |
| |
| |
| add, sub = calc(8, 2) |
| print(f"{add}, {sub}") |
Go
使用元组。
| func calc(a int, b int) (int, int) { |
| return a + b, a - b |
| } |
| func main() { |
| add, sub := calc(8, 2) |
| fmt.Println(add, sub) |
| } |
C++
使用元组和结构化绑定声明。
| tuple<int, int> calc(int a, int b) |
| { |
| return make_tuple(a + b, a - b); |
| } |
| int main() |
| { |
| auto [add, sub] = calc(8, 2); |
| cout << add << ", " << sub << endl; |
| return 0; |
| } |
| |
| 注意: |
| make_tuple不能写成 make_ |
| tuple<int, int>(...), 要缺省掉模板参数,让编译器自动推导。msvc编译器会报错,编译不过。 |
#include <iostream>
#include <tuple>
#include <functional>
std::tuple<int, int> f() // this function returns multiple values
{
int x = 5;
return std::make_tuple(x, 7); // return {x,7}; in C++17
}
int main()
{
// heterogeneous tuple construction
int n = 1;
auto t = std::make_tuple(10, "Test", 3.14, std::ref(n), n);
n = 7;
std::cout << "The value of t is " << "("
<< std::get<0>(t) << ", " << std::get<1>(t) << ", "
<< std::get<2>(t) << ", " << std::get<3>(t) << ", "
<< std::get<4>(t) << ")\n";
// function returning multiple values
int a, b;
std::tie(a, b) = f();
std::cout << a << " " << b << "\n";
}
Ruby
使用解构赋值。
| def calc(a, b) |
| return [a + b, a - b] |
| end |
| a, b = calc(8, 2) |
| puts "#{a}, #{b}" |
Swift
| func calc(a: Int, b: Int) -> (Int, Int) { |
| return (a + b, a - b) |
| } |
| let (add, sub) = calc(a:8, b:2) |
| print("\(add), \(sub)") |
F#
| let calc a b = |
| a + b, a - b |
| |
| [<EntryPoint>] |
| let main argv = |
| let add, sub = calc 8 2 |
| printfn "%d, %d" add sub |
| 0 |
Groovy
| def calc(a, b) { |
| return [a + b, a - b] |
| } |
| def (add, sub) = calc(8, 2) |
| println("${add}, ${sub}") |
Perl 6
| sub calc($a, $b) { |
| return ($a + $b, $a - $b); |
| } |
| my ($add, $sub) = calc(8, 2); |
| print "$add, $sub"; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2014-12-30 Android 友盟SDK 终极解决报错:SocialSDK_QQZone_2.jar contains native libraries that
2011-12-30 剖析Android Traceview效率工具及两种建SDcard方式
2011-12-30 获取证书