关于“范围”在艾达

发布于 2024-12-20 07:08:16 字数 205 浏览 2 评论 0原文

Ada 中的以下源代码行

type Airplane_ID is range 1..10;

可以写为

type Airplane_ID is range 1..x;

,其中 x 是变量?我问这个是因为我想知道 x 的值是否可以修改,例如通过文本输入。提前致谢。

The following source code line in Ada,

type Airplane_ID is range 1..10;

, can be written as

type Airplane_ID is range 1..x;

, where x is a variable? I ask this because I want to know if the value of x can be modified, for example through text input. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

隱形的亼 2024-12-27 07:08:16

不,范围的边界都必须是静态表达式。

但是您可以声明具有动态边界的子类型:

X: Integer := some_value;
subtype Dynamic_Subtype is Integer range 1 .. X;

No, the bounds of the range both have to be static expressions.

But you can declare a subtype with dynamic bounds:

X: Integer := some_value;
subtype Dynamic_Subtype is Integer range 1 .. X;
对你再特殊 2024-12-27 07:08:16

可以type Airplane_ID is range 1..x;写入其中x是a
多变的?我问这个是因为我想知道 x 的值是否可以
修改,例如通过文本输入。

我假设你的意思是改变 x 的值会以动态的方式改变范围本身;如果是这样,那么严格来说,不……但这并不是全部答案。

你可以这样做:

Procedure Test( X: In Positive; Sum: Out Natural ) is
  subtype Test_type is Natural Range 1..X;
  Result : Natural:= Natural'First;
 begin
   For Index in Test_type'range loop
     Result:= Result + Index;
   end loop;

   Sum:= Result;
 end Test;

Can type Airplane_ID is range 1..x; be written where x is a
variable? I ask this because I want to know if the value of x can be
modified, for example through text input.

I assume that you mean such that altering the value of x alters the range itself in a dynamic-sort of style; if so then strictly speaking, no... but that's not quite the whole answer.

You can do something like this:

Procedure Test( X: In Positive; Sum: Out Natural ) is
  subtype Test_type is Natural Range 1..X;
  Result : Natural:= Natural'First;
 begin
   For Index in Test_type'range loop
     Result:= Result + Index;
   end loop;

   Sum:= Result;
 end Test;
独行侠 2024-12-27 07:08:16

不可以。Ada 范围声明必须是常量。

No. An Ada range declaration must be constant.

梦里的微风 2024-12-27 07:08:16

正如其他答案所提到的,您可以按照自己想要的方式声明范围,只要它们是在某种块中声明的 - “声明”块,或者过程或函数;例如:

with Ada.Text_IO,Ada.Integer_Text_IO;
use Ada.Text_IO,Ada.Integer_Text_IO;

procedure P is
   l : Positive;
begin
   Put( "l =" ); 
   Get( l );
   declare
      type R is new Integer range 1 .. l;
      i : R;
   begin
      i := R'First;
      -- and so on
   end;
end P;

As the other answers have mentioned, you can declare ranges in the way you want, so long as they are declared in some kind of block - a 'declare' block, or a procedure or function; for instance:

with Ada.Text_IO,Ada.Integer_Text_IO;
use Ada.Text_IO,Ada.Integer_Text_IO;

procedure P is
   l : Positive;
begin
   Put( "l =" ); 
   Get( l );
   declare
      type R is new Integer range 1 .. l;
      i : R;
   begin
      i := R'First;
      -- and so on
   end;
end P;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文