为什么在Ada中没有像+ =,-=或++这样的(扩展分配)运算符? [英] Why are there no (augmented assignment) operators like +=, -= or ++ in Ada?

查看:100
本文介绍了为什么在Ada中没有像+ =,-=或++这样的(扩展分配)运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么没有像+=-=++-=<<=x ? y : z这样的运算符(不是增补赋值 .. .)在艾达(Ada)?许多其他语言(C,C ++,C#,Java,Perl)也有它们.

I'm wondering why there are no operators like +=, -=, ++, -=, <<= or x ? y : z (not an augmented assignment ...) in Ada? Many other languages (C, C++, C#, Java, Perl) have them.

-示例(C/C ++/...):

int a = 3;

a += 4; /* A */
// long: a = a + 4

a++; /* B */
// long: a = a + 1

a = ( a > 3 ? 10 : 5 ); /* C */
// long: ' if a > 3 then a = 10 else a = 5'

-示例(Ada):

a : integer := 3;

a := a + 4;   -- A --
a := a + 1;   -- B --

if a > 3 then -- C --
    a := 10;
else
    a := 5;
end if;

(示例没有意义-仅用于演示)

是因为...

  • 运算符重载(但是C ++也有这样的机制)?
  • 可读性?
  • 技术原因/局限性?
  • 这只是使这些表达式更短而不是编程真正需要的一个技巧吗?
  • Ada中的赋值运算符是:=而不是=(所以+=-> +=:)?
  • Operator-overloading (but C++ has such a mechanism too)?
  • Readability?
  • Technical reasons / limitations?
  • It's only a trick for making those expressions shorter and not really required for programming?
  • The assignment operator in Ada is := and not = (so += -> +=:)?

推荐答案

因为与其他一些语言相比,Ada的设计与数学的联系更为紧密……所以……

Because the design of Ada was taken much more closely from mathematics than some other languages... And so...

分配不是运营商

操作员具有特定的属性-他们对返回结果的数量进行操作-同时使数量本身保持不变.

Operators have specific properties - they operate on quantities returning a result - while leaving the quantities themselves unchanged.

这很重要-严格遵循对操作员"的理解,由于语义更容易预测,因此您可以进行很多优化.本质上,操作员没有副作用.您可以重复它们,也可以排除重复的结果,这样就可以在不更改结果的情况下自由地对表达式进行重新排序.

This is important - stick rigorously to this understanding of an "operator" and you make a lot of optimisations possible because the semantics are much more predictable. Essentially, operators don't have side effects. You can repeat them or factor out repeated ones, and you have a lot more freedom to reorder expressions without changing their results.

如果您错误地分配了操作员,...好吧,基本上,您很困惑. 仅一个具有副作用的操作员"就意味着您失去了所有操作员的宝贵财产……这是什么?一些符号上的便利,一个非常丰富的bug繁殖地,并且没有额外的性能或效率.

If you mistake assignment for an operator, ... well, basically you're screwed. Just ONE "operator" with side effects means you lose valuable properties for ALL operators ... for what? some notational convenience, a hugely fertile breeding ground for bugs, and no extra performance or efficiency.

偶然地,当我最近不得不在GCC内四处摸索时,我在它的表达式分析器中发现了一个函数,该函数明确破坏了a++的(中间表示)并将其内部转换为(c11的中间表示),因此,较短的形式实际上是似乎没有任何效率!

Incidentally when I had to poke around inside GCC recently I found a function in its expression analyser that explicitly broke (intermediate representation for) a++ and transformed it internally into (intermediate representation for) a = a + 1; So the shorter form really doesn't appear to be any more efficient!

对函数使用相同的原理(在Ada中比在VHDL中严格地更少)-它们只是换一种说法,而纯函数(在VHDL中,每个函数在其声明中都没有不纯"一词!)副作用.

The same rationale applies (less strictly in Ada than VHDL) to functions - they are just operators in another guise, and pure functions (in VHDL that's every function without the word "impure" in its declaration!) don't have side effects.

这也是为什么Ada同时具有函数和过程的原因:函数,运算符和表达式本质上是相似的(理想情况下是无状态和无副作用的);程序,赋值和语句是一个单独的类别(过程调用和赋值是语句的形式).

Which is also why Ada has both functions and procedures : functions, operators and expressions are essentially similar (and ideally, stateless and side-effect free); procedures, assignments and statements are a separate category (procedure calls and assignments are forms of statement).

将概念分开并为每个任务使用适当的概念,这将使您可以理解清晰的程序,并且可以按照您的意图进行操作很长的路要走...

Separating the concepts and using the appropriate one for each task goes a long way to making clear programs that you can understand and probably do what you intended...

Oh和Ada-2012终于以if-和case-expressions赶上了VHDL-2008和Algol-W(1963)...

Oh and Ada-2012 has finally caught up with VHDL-2008 and Algol-W (1963) with if- and case-expressions...

a := (if a > 3 then 10 else 5);
-- to my eyes MUCH more readable than ?: especially in multiple if-exprs!

b := (case a is 
      when 3 => 5;
      when 6|7 => 10;
      when others => 0);

很明显,这里的分配仍然是语句...

It is obvious that the assignments here are still statements...

只需确保:

分配不是运营商

Ada的设计师对通常情况下的印象非常深刻,而且通常非常清楚,这并不影响完整性,而这只会导致混乱.虽然添加了更新的语言功能,但由于编译器技术的发展足以使其可靠,因此这一直是一个谨慎的过程,Ada-83子集实际上仍然完好无损.

Ada's designers had an impressive and usually VERY clear grasp of what was possible without compromising integrity and what would just lead to a mess. While newer language features have been added, as compiler techniques developed enough to make them reliable, it has always been a cautious process and the Ada-83 subset is still there virtually intact.

这篇关于为什么在Ada中没有像+ =,-=或++这样的(扩展分配)运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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