Ada: Why is "A" .. "F" "not discrete?

Program text

type T is array ("A" .. "F") of Integer; 

Compiler console output

 hello.adb:4:22: discrete type required for range 

Question

If my understanding is correct, article 9 of chapter 3.6 of the Ada reference manual causes the compiler to raise a compilation error:

Each index_subtype_definition or discrete_subtype_definition in the array_type_definition file defines an index subtype; its type (index type) must be discrete.

Therefore, why exactly "A" .. "F" not discrete ? What does discrete mean?

Background Information

The following are the syntax requirements for array type definitions. Source: Ada Reference Guide

 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) 
+5
source share
2 answers

It:

 "A" .. "F" 

satisfies the syntax of a range ; it consists of simple_expression , followed by .. , and then another simple_expression . Therefore, this is not a syntax error.

It is still invalid; in particular, this is a semantic error. Syntax is not the only thing that determines whether a piece of code is valid or not. For example, "foo" * 42 is a syntactically valid expression, but it is semantically invalid because there is no * operator for a string and an integer (unless you write your own).

A discrete type is either an integer type or an enumeration type. Integer , Character and Boolean are examples of discrete types. Floating-point types, array types, pointer types, record types, etc. They are not discrete types, therefore expressions of these types cannot be used in the range for discrete_subtype_indication .

It:

 type T is array ("A" .. "F") of Integer; 

probably supposed:

 type T is array ('A' .. 'F') of Integer; 

String literals are of type String , which is an array type. Character literals are of type Character , which is an enumeration type and therefore a discrete type.

In response to another comment, you wrote:

Unfortunately, I cannot replace string literals with character literals and recompile the code ...

If so, then this is pretty unfortunate. The code you posted is simply invalid; it will not compile. Your only options are to change it or not to use it.

+8
source

Ermm ... I think he is trying to tell you that you cannot use string literals to indicate ranges. You may have used an alphabetic character.

Link:


After all, the above suggestions explicitly require the use of string_literal

You misunderstood the Ada syntax specifications. In particular, you missed this product:

 name ::= simple_name | character_literal | operator_symbol | indexed_component | slice | selected_component | attribute 
+4
source

Source: https://habr.com/ru/post/1202513/


All Articles