为什么赋值语句返回一个值? [英] Why do assignment statements return a value?

查看:100
本文介绍了为什么赋值语句返回一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是允许的:

  INT A,B,C;
A = B = C = 16;字符串s = NULL;
而((S =你好)!= NULL);

据我了解,分配 s =你好; 只能引起你好被分配到取值,但操作应该不会返回任何值。如果这是真的话,那么((S =你好)!= NULL)会产生一个错误,因为将比较不了了之。

什么是背后的推理允许赋值语句返回一个值?


解决方案

  

据我了解,赋值S =你好;应该仅仅导致你好将被分配给S,但该操作不应该返回任何值。


你的理解是100%不正确。 你能解释一下为什么你认为这个虚假的东西?


  

什么是背后的推理允许赋值语句返回一个值?


首先,分配的语句的不产生价值。分配的前pressions 的生产值。转让前pression是一个合法的声明;只有前pressions这是在C#法律声明一把:其中一份声明中预计可用于例如建筑,递增,递减,调用和分配前pressions

有只有一种在C#中前pression的不产生某种价值,即东西调用该类型为返回void。每一个其他类型的前pression的生产值或变量或引用或属性访问或事件访问,等等。

请注意,所有的前pressions这是合法的,因为声明的为它们的副作用非常有用的。这是关键的洞察力在这里,我想也许你的直觉因为那任务应该是声明,而不是前pressions。理想情况下,我们不得不每条语句只有一个副作用,且无副作用,在离pression。它的的有些奇怪的是,副作用的code,可以在离pression背景下使用的。

背后允许此功能的原因是因为:(1)它是经常方便(2)它是在C语言的语言习惯。

也许有人会注意到这个问题已经央求:为什么在类似C语言的这种习惯。

您得问丹尼斯·里奇是肯定的,但我的猜测是,转让几乎总是留下的刚分配的一个寄存器中的值。 C是一个非常接近机器之类的语言。它似乎是合理和用C有是一个语言特性基本上是指设计保持继续使用,我刚分配的价值。这是很容易编写此功能的code发生器;你只是继续使用存储已分配的值的寄存器。

This is allowed:

int a, b, c;
a = b = c = 16;

string s = null;
while ((s = "Hello") != null) ;

To my understanding, assignment s = "Hello"; should only cause "Hello" to be assigned to s, but the operation shouldn’t return any value. If that was true, then ((s = "Hello") != null) would produce an error, since null would be compared to nothing.

What is the reasoning behind allowing assignment statements to return a value?

解决方案

To my understanding, assignment s = "Hello"; should only cause "Hello" to be assigned to s, but the operation shouldn’t return any value.

Your understanding is 100% incorrect. Can you explain why you believe this false thing?

What is the reasoning behind allowing assignment statements to return a value?

First off, assignment statements do not produce a value. Assignment expressions produce a value. An assignment expression is a legal statement; there are only a handful of expressions which are legal statements in C#: instance construction, increment, decrement, invocation and assignment expressions may be used where a statement is expected.

There is only one kind of expression in C# which does not produce some sort of value, namely, an invocation of something that is typed as returning void. Every other kind of expression produces a value or variable or reference or property access or event access, and so on.

Notice that all the expressions which are legal as statements are useful for their side effects. That's the key insight here, and I think perhaps the cause of your intuition that assignments should be statements and not expressions. Ideally, we'd have exactly one side effect per statement, and no side effects in an expression. It is a bit odd that side-effecting code can be used in an expression context at all.

The reasoning behind allowing this feature is because (1) it is frequently convenient and (2) it is idiomatic in C-like languages.

One might note that the question has been begged: why is this idiomatic in C-like languages?

You'd have to ask Dennis Ritchie to be sure, but my guess is that an assignment almost always leaves behind the value that was just assigned in a register. C is a very "close to the machine" sort of language. It seems plausible and in keeping with the design of C that there be a language feature which basically means "keep on using the value that I just assigned". It is very easy to write a code generator for this feature; you just keep on using the register that stored the value that was assigned.

这篇关于为什么赋值语句返回一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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