Ada 中的整数到自定义类型的转换
Integer to custom type conversion in Ada
我有一个由某种类型的对象组成的数组,按类型索引 index
:
type index is new Integer range 1..50;
type table is new Array(index) of expression;
现在,我需要访问这些表达式之一,具体取决于用户通过键盘输入的内容。为此,我执行以下操作:
c: Character;
get(c);
s: String := " ";
s(1) := c;
我终于可以将字符转换为类型 Integer
:
i: Integer;
i := Integer'Value(s);
现在,我有了用户想要访问的值的位置,但是 Ada 不允许你访问 table
,因为它是由 index
而不是 [=14] 索引的=],它们是不同的类型。
根据用户输入访问表达式的最佳解决方案是什么?
type index is new Integer range 1..50;
type table is new Array(index) of expression;
您不需要(也不可能)在 table
的声明中使用 new
关键字。
c: Character;
get(c);
s: String := " ";
s(1) := c;
最后两行可以写成:
S: String := (1 => C);
(假设 C
在声明 S
的位置可见并初始化)。
i: Integer;
i := Integer'Value(s);
这不是 "cast"。艾达没有演员表。它甚至不是类型转换。但我明白你的意思;如果 C = '4'
,则 S = "4"
,然后 Integer'Value(S) = 4
。 (你应该考虑如果 C
的值不是十进制数字怎么办;这将导致 Integer'Value(S)
提高 Constraint_Error
。)
Now, I have the position of the value the user want to access, but Ada
doesn't let you access to table
, because it is indexed by index
and not Integer
, which are different types.
简单:不要使用不同的类型:
I: Index := Index'Value(S);
我有一个由某种类型的对象组成的数组,按类型索引 index
:
type index is new Integer range 1..50;
type table is new Array(index) of expression;
现在,我需要访问这些表达式之一,具体取决于用户通过键盘输入的内容。为此,我执行以下操作:
c: Character;
get(c);
s: String := " ";
s(1) := c;
我终于可以将字符转换为类型 Integer
:
i: Integer;
i := Integer'Value(s);
现在,我有了用户想要访问的值的位置,但是 Ada 不允许你访问 table
,因为它是由 index
而不是 [=14] 索引的=],它们是不同的类型。
根据用户输入访问表达式的最佳解决方案是什么?
type index is new Integer range 1..50;
type table is new Array(index) of expression;
您不需要(也不可能)在 table
的声明中使用 new
关键字。
c: Character;
get(c);
s: String := " ";
s(1) := c;
最后两行可以写成:
S: String := (1 => C);
(假设 C
在声明 S
的位置可见并初始化)。
i: Integer;
i := Integer'Value(s);
这不是 "cast"。艾达没有演员表。它甚至不是类型转换。但我明白你的意思;如果 C = '4'
,则 S = "4"
,然后 Integer'Value(S) = 4
。 (你应该考虑如果 C
的值不是十进制数字怎么办;这将导致 Integer'Value(S)
提高 Constraint_Error
。)
Now, I have the position of the value the user want to access, but Ada doesn't let you access to
table
, because it is indexed byindex
and notInteger
, which are different types.
简单:不要使用不同的类型:
I: Index := Index'Value(S);