为什么单元素记录汇总需要命名关联? [英] Why is named association required for a one-element record aggregate?

查看:77
本文介绍了为什么单元素记录汇总需要命名关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个聚合仅包含一个元素时,如下所示,位置表示法会导致编译错误,我们只能使用命名的表示法。为什么?

When an aggregate contains only one element, as below, positional notation results in a compilation error and we have to use named notation only. Why?

type singleton is record
   v : integer;
end record;

v1 : singleton := (0);

产生编译器消息

check.adb:6:23: positional aggregate cannot have one component
check.adb:6:23: write instead "V => ..."
gnatmake: "check.adb" compilation error

这是可以的:

whereas this is OK:

v2 : singleton := (v => 0);


推荐答案

围绕表达式的括号是多余的,因此(0)= 0,它是一个整数,而不是数组集合

Parentheses round an expression are redundant, so (0) = 0 and it's an integer not an array aggregate.

因此,对于单元素聚合的特殊情况,需要命名关联才能将聚合与简单值区分开。

So, for the special case of a one-element aggregate, named association is required to distinguish an aggregate from a simple value.

将此值与(0,0)进行比较,后者只能是一个总计;因此没有歧义。

Contrast this with (0,0) which can only be an aggregate; therefore there is no ambiguity.

即使在上下文中问题,这对人类程序员来说是显而易见的,并非总是如此。

Even though in the context of the question, it's obvious to a human programmer which is intended, that will not always be the case.

Consi在多维数组中将一个元素汇总为记录的一个字段;可能存在一些歧义,编译器无法解决(至少在读取更多的源文件之前!),并使阅读该程序的人很难过。

Consider a one-element aggregate in a multi-dimensional array which is one field of a record; there can be ambiguities that the compiler cannot resolve (at least, before reading a whole lot more of the source file!) and would make life pretty difficult for anyone reading the program.

这篇关于为什么单元素记录汇总需要命名关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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