Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Derived types and sub types in Ada

Tags:

types

ada

What are the differences?

like image 649
NHans Avatar asked Apr 20 '10 18:04

NHans


People also ask

What are subtypes in Ada?

A subtype is a type together with a constraint; a value is said to belong to a subtype of a given type if it belongs to the type and satisfies the constraint; the given type is called the base type of the subtype.

What is the difference between types and subtypes?

The %TYPE attribute lets you declare a constant, variable, field, or parameter to be of the same data type a previously declared variable, field, record, nested table, or database column. For the other hand, a SUBTYPE does not introduce a new type; rather, it places an optional constraint on its base type.

What is a derived type?

A derived type is formed by using one or more basic types in combination. Using derived types, an infinite variety of new types can be formed. The array and structure types are collectively called the aggregate types. Note that the aggregate types do not include union types, but a union may contain an aggregate member.

Which of the following are structural data types in Ada?

As for Ada, records and arrays would be language-supported data structures. Packages are also a kind of a data structure.


2 Answers

First of all, terminology: it's "Ada", not "ADA" -- it's named after "Ada Lovelace"; it is not an acronym.

A subtype is compatible with its base type, so you can mix operands of the base type with operands of the base type. For example:

subtype Week_Days is Integer range 1..7;

Since this is a subtype, you can (for example) add 1 to a weekday to get the next weekday.

A derived type is a completely separate type that has the same characteristics as its base type. You cannot mix operands of a derived type with operands of the base type. If, for example, you used:

type Week_Day is new Integer range 1..7;

Then you would not be able to add an integer to a weekday to get another weekday. To do manipulations on a derived type, you'd normally define those manipulations yourself (e.g., create a package). At the same time, a derived type does "inherit" all the operations of its base type (even some that may not make sense) so you do still get addition.

like image 159
Jerry Coffin Avatar answered Oct 03 '22 08:10

Jerry Coffin


From Wikibooks:

Subtypes of a given type will be compatible with each other.

A derived type is a new, full-blown type created from an existing one. Like any other type, it is incompatible with its parent; however, it inherits the primitive operations defined for the parent type.

like image 42
Marc C Avatar answered Oct 03 '22 08:10

Marc C