Mul*_*ync 5 arrays types ada range
type T is array ("A" .. "F") of Integer;
Run Code Online (Sandbox Code Playgroud)
hello.adb:4:22: discrete type required for range
Run Code Online (Sandbox Code Playgroud)
如果我的理解是正确的,那么Ada参考手册第3.6章第9 节就是编译器引发编译错误的原因:
array_type_definition中的每个index_subtype_definition或discrete_subtype_definition定义索引子类型; 它的类型(索引类型)应该是离散的.
因此,为什么"A" .. "F"不discrete呢?究竟是discrete什么意思?
下面引用了数组类型定义的语法要求.资料来源:Ada参考手册
array_type_definition ::= unconstrained_array_definition | constrained_array_definition
constrained_array_definition ::= array (discrete_subtype_definition {, discrete_subtype_definition}) of component_definition
discrete_subtype_definition ::= discrete_subtype_indication | range
range ::= range_attribute_reference | simple_expression .. simple_expression
simple_expression ::= [unary_adding_operator] term {binary_adding_operator term}
term ::= factor {multiplying_operator factor}
factor ::= primary [** primary] | abs primary | not primary
primary ::= numeric_literal | null | string_literal | aggregate | name | qualified_expression | allocator | (expression)
Run Code Online (Sandbox Code Playgroud)
这个:
"A" .. "F"
Run Code Online (Sandbox Code Playgroud)
确实满足所述语法的一个range; 它由一个simple_expression,然后..,紧接着又simple_expression.所以这不是语法错误.
它仍然无效; 特别是它是一个语义错误.语法不是唯一决定一块代码是否有效的东西.例如,"foo" * 42是一个语法上有效的表达式,但它在语义上是无效的,因为没有*字符串和整数的运算符(除非你自己编写).
离散类型是整数类型或枚举类型.Integer,Character和,Boolean是离散类型的例子.浮点类型,数组类型,指针类型,记录类型等不是离散类型,因此这些类型的表达式不能在a的范围内使用discrete_subtype_indication.
这个:
type T is array ("A" .. "F") of Integer;
Run Code Online (Sandbox Code Playgroud)
可能应该是:
type T is array ('A' .. 'F') of Integer;
Run Code Online (Sandbox Code Playgroud)
字符串文字是类型String,它是一种数组类型.字符文字属于类型Character,它是枚举类型,因此是离散类型.
你在另一个答案的评论中写道:
不幸的是我无法用字符文字替换字符串文字并重新编译代码......
如果是这样的话,那很不幸.您发布的代码无效; 它不会编译.您唯一的选择是修改它或不使用它.