Procedural Programming + Ada, Lesson 5 – Basic Types & Attributes

Tram Ho

Ada ‘s design has a strong emphasis on particularly powerful styling. If to tell all the detailed elements that Ada provides around this feature, we will probably need a few articles with a fairly thick knowledge density. However, to keep things simple and get a better overview, in this article we’ll just go through some of the same basic data types used in other languages.

Basic Types

[ Arithmetic Values ​​]

  • Integer – represents integer values.
  • Float – represents real numeric values ​​with 32bit storage width.
  • Long_Float – equivalent to double in C , Java , etc.. and number in JavaScript with 64bit storage width.

There is a remarkable feature here, that Ada will never implicitly perform the type conversion even if the compiler has enough data about the data types of the variables.

[ Logical Comment ]

  • Boolean – represents the logically recognized values TRUE or FALSE .

[ Content Text ]

  • Character – denotes single characters consisting of letters and special characters.
  • String – represents a fixed length string of characters.
  • Bounded_String – stores a string of variable length but with the maximum length defined.
  • Unbounded_String – stores strings of non-fixed length and unlimited maximum length.

Hmm.. perhaps this is the point to note, we will need to specify the operations we want to perform with a string to choose the appropriate data type in Ada .

[ Meaningless Value ]

In particular, Ada does not use null to indicate the absence of data at any given identifier. The only place where null is used is in pointer variables, which we will talk about in another article.

Subtypes

By the way, doing an example of Ada ‘s style feature with String . We are getting an error message related to the word subtype – roughly translated as a data type with a smaller margin defined from a pre-existing data type.

As we can see, in the result of compiling the code we received a warning on the line P := P - 1; will overflow the stored value. However, in line P := I - 8; then operational logic is completely allowed, unlike in the case of Float and Integer .

And here we can understand that the subtype will not create a new data type, but create a smaller constraint than the original data type. We will be able to use calculation operations with variables of the original data type normally. This type of subtype definition is very useful if we want to bind a variable that stores the result of a calculation.

Attributes

attribute properties in Ada can be understood as methods designed to work with elements such as the Type data Type , Value values, and Sub-program subroutines. We used the attribute right from the Hello, World! in the opening post. It’s Image , a generic property of data types that converts a value of that type to a String .

The apostrophe character ' here is used quite similar to the execution . when we use a static method in JavaScript . And to keep the goal of the article around introducing concepts, we’ll just note a few common attribute :

  • Type'Image (Value) – converts a Value value of type Type to a String .
  • Type'Value (String) – converts a String string to a Value of type Type .
  • Type'First – returns the first value in the range of type Type .
  • Type'Last – returns the last value in the range of type Type .

Here is the complete list of attribute attribute that Ada provides: Wikipedia - Attribute List

In the next article, we will talk about defining new data types ourselves.

(unpublished) [Procedural Programming + Ada] Lesson 6 – Arrays & Records

Share the news now

Source : Viblo