Search code examples
typescompiler-errorsdeclarationadasubtype

Ada Type Declaration Semantics


I am taking Ada in college. Today my professor posed a question to us. In the following bit of code there are three type declarations. The third one doesn't compile with an error 'missing new'.

I am assuming that the 'Type' keyword lets the compiler know that we are about define a storage class so my questions are:

what are the semantic differences between the declarations?
If the keyword Integer is omitted does Ada assume the universal integer type when the range is specified? This seems like an obvious and logical outcome. Furthermore when the keyword 'Integer' is before 'range' is new then required?

I hope this wasn't ambiguous, I did some research but can't seem to find an exact answer, or maybe i'm to much of a newbie with Ada to understand what I have found. I really would like to understand what is happening below.

with Ada.Text_IO; use ada.Text_IO;
    procedure any is
    type abc is new Integer range 1..10;
    num : abc;
    type def is range 1..10;
    num2 : def;
    type xyz is Integer range 1..10;
    num3 : xyz;
      begin
      num := 5;
      num2 := 6;
      num3 := 7;
 end any;

Solution

  • In the first declaration, you are declaring a new type, distinct from Integer, but inheriting a few properties from it (primitive functions and operators for example, not that it matters).

    For every practical matters, the types abc and def are semantically equivalent in this case. However, if you had primitive operations on your base type, it would be different.

    You cannot really consider that the ranges' bounds have types in this case, but if there is a base type, they must fulfill the constraint of being in the range of the base type.

    Now xyz is simply incorrect, and Integer does not mean anything here. It could mean either of two things:

    • You really wanted to declare a new type derived from integer. In this case you're missing the new keyword.
    • You wanted to declare a subtype of integer. A subtype is basically equivalent to its base type as far as name resolution and static type matching are concerned, but will have different constraints at execution.

    Furthermore when the keyword 'Integer' is before 'range' is new then required?

    Integer is not a keyword, it's a type defined in the standard package. In this declaration

    type A is Integer range 1 .. 10;
    

    Integer is a reference to a type where Ada does not expect one. It's expecting either a type definition, either a base type.