简体   繁体   English

Ada中不同类型之间的数组切片

[英]Array slicing between different types in Ada

An Ada tutorial I'm reading says that this is a valid thing to do in Ada: 我正在阅读的Ada教程说,这是在Ada中要做的一件事:

Large : array (0 .. 100) of Integer;
Small : array (0 .. 3) of Integer;

-- extract section from one array into another.
Small(0 .. 3) := Large(10 .. 13);

However, when I actually try it , it doesn't work. 但是,当我实际尝试时 ,它不起作用。

I'm using ideone's online Ada environment to play with this stuff, for simplicity (link above). 为了简单起见,我正在使用ideone的在线Ada环境来玩这个东西(上面的链接)。 Is the tutorial wrong, or is it referring to a version of Ada which ideone doesn't implement? 本教程是否错误,或者是指ideone未实现的Ada版本?

The tutorial is wrong. 本教程是错误的。

This will compile, though: 但这会编译:

type Integer_Array is array (Natural range <>) of Integer;
Large : Integer_Array (0 .. 100);
Small : Integer_Array (0 .. 3);

...
Small (0 .. 3) := Large (10 .. 13);

The details: In my example, Integer_Array is a type (actually a type and a "first subtype"), and Integer_Array(0..100) and Integer_Array(0..3) are constrained subtypes . 详细信息:在我的示例中, Integer_Array是一个类型 (实际上是一个类型和一个“第一Integer_Array(0..100)类型”), Integer_Array(0..100)Integer_Array(0..3)是受约束的子类型 Array types in Ada specify the type(s) of the index(es) and the type of the element, but not the bounds; Ada中的数组类型指定索引的类型和元素的类型,但不指定边界。 bounds are specified by subtypes, specifically constrained subtypes. 边界由子类型指定,特别是受约束的子类型。 (That doesn't mean that you need a subtype declaration; a type declaration can cause both a type and a constrained subtype to be defined.) (这并不意味着您需要一个subtype声明; type声明可以导致定义一个类型和一个受约束的子类型。)

An Ada assignment requires (usually) that the source and destination be of the same type, but they can be different subtypes. Ada分配(通常)要求源和目标是同一类型,但它们可以是不同的子类型。 So in my example, both the source and destination are subtypes of Integer_Array , and therefore the assignment meets this requirement. 因此,在我的示例中,源和目标都是Integer_Array子类型,因此分配满足此要求。

In the original example, the declarations of Large and Small define new anonymous array types. 在原始示例中, LargeSmall声明定义了新的匿名数组类型。 Each type is unbounded, and the declarations also define bounded subtypes that are used as the variables' subtypes. 每种类型都是无界的,并且声明还定义了用作变量子类型的有界子类型。 But the important thing here is that two distinct types are defined, and you can't assign one type to the other. 但是这里重要的是定义了两种不同的类型,您不能将一种类型分配给另一种类型。 Furthermore, since the types are unnamed, you can't use a type conversion to make the assignment work, as you could in an example like this: 此外,由于类型是未命名的,因此您不能使用类型转换来使分配工作正常进行,就像在这样的示例中那样:

type Integer_Array is array (Natural range <>) of Integer;
Large : array (0 .. 100) of Integer;
Small : Integer_Array (0 .. 3);

...
Small (0 .. 3) := Integer_Array (Large (10 .. 13));

Here, Large and Small again have two distinct types, but a type conversion is possible because the destination type is named. 此处, LargeSmall再次具有两种不同的类型,但是可以进行类型转换,因为已命名目标类型。

The moral here is that it's better to use named array types. 这里的道理是最好使用命名数组类型。 Variable declarations like 像这样的变量声明

Large : array (0 .. 100) of Integer;

can be good enough in some instances; 在某些情况下可能足够好; but as this example shows, there are certain things you can't do with them. 但如本例所示,某些事情您无法使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM