在C ++和Java中只有一个返回值的原因是什么? [英] What is the reason behind having only one return value in C++ and Java?

查看:47
本文介绍了在C ++和Java中只有一个返回值的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常我会看一眼这个事实,只是接受它为这就是轮子旋转的方式",但是今天我不知道这是从哪里来的:为什么一个函数只有一个返回(参考)值?>

Normally I glance over this fact and just accept it as "this is how the wheel spins" but today I wonder where this comes from: Why does a function only have one return (reference)value?

为什么函数不能返回多个值很难还是不明智?是否由于对象而引起,您应该如何期望始终以对对象的引用形式返回数据包?

Why can't, would it be hard, or not wise for a function to return multiple values? Is it because of objects and how you should expect that a package of data should always be returned in the form of a reference to an object?

如果您将声明的函数视为合同,其中说明了如何调用函数,应采用的参数以及其具有的返回值类型,那么我看不到仅具有一个返回值的逻辑您可以反过来应用相同的逻辑(从而为多个返回值创建合约).

If you see a declared function as a contract which states how a function can be called, which parameters it should take, and which return value type it has, then I don't see the logic behind having only one return value since you could apply the same logic the other way around (thus creating a contract for more than one return value).

我所看到的基于逻辑的唯一原因是,如果一个函数返回的东西多于1个,它也应该出于1个以上的原因操纵1个以上的东西,这违背了函数只应该做的事情.如果那件事是操纵一个对象并返回另一个对象,那将是有道理的,那么您可以使用操纵的对象返回一个参考值.

The only reason based on logic I can see is that if a function returns more than 1 thing, it should also manipulate more than 1 thing for more than 1 reason which is against the philosophy that a function should do only one thing. If that one thing is manipulating an object and returning an other object it would make sense and you can return a reference value with the manipulated object..

那么为什么存在这个限制呢?

So why exactly does this limitation exist?

推荐答案

每当您有多个值 x1 ... xN 时,您就会轻易地拥有元组(x1,...,xN),它是单个(尽管是复合值)值.如果您的语言中没有元组,请随意使用任何聚合类型( struct class ,数组,其他集合).因此,从这个角度来看,返回多个"值和返回单个值是完全等效的.

Whenever you have multiple values x1 ... xN, you trivially have the tuple (x1, ..., xN) which is a single (albeit composite) value. If you don't have tuples in your language, use any aggregate type (struct, class, arrays, other collections) at will. So from this perspective, returning "multiple" values and returning a single value is completely equivalent.

那只是意味着您只需要一个,但是为什么要省略另一个?

That just means that you only need one, but why omit the other?

首先,无论如何,您都需要聚合类型,因此可以在同时具有两种或仅具有聚合类型之间进行选择.其次,如果一个函数可以返回多个值",那么您将面临一个语义难题:突然一个表达式不能求值(我们之前已经非常明确地定义了值),这就是这种新的不同类别的事物,称为多个值".现在,此结果的静态类型是什么?该程序可以做什么,不可以做什么?在实施方面意味着什么?

First, you need aggregate types anyway, so the choice is between having both or only having aggregate types. Second, if a function can return "multiple values", you face a semantic conundrum: Suddenly an expression does not evaluate to value (which we have previously defined very specifically), it's this new, different category of thing called "multiple values". Now what is the static type of this result? What can the program do and not do with it? What does it mean in implementation terms?

您当然可以人为地回答这些问题,但是任何明智的方法都将等于元组类型.忽略这一点会使您看不到非常有用的观点,并且拒绝使它们成为一等值可能比说这些是元组类型,可以像这样构造和像这样解构"那样复杂.

You can of course artificially answer these questions, but any sensible approach will just amount to tuple types. Ignoring that makes you blind to a very useful perspective, and refusing to make them first-class values is probably more complicated than saying "these are tuple types, they can be constructed like this and deconstructed like this".

这篇关于在C ++和Java中只有一个返回值的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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