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 with32bit
storage width.Long_Float
– equivalent todouble
inC
,Java
, etc.. andnumber
inJavaScript
with64bit
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.
1 2 3 4 5 6 7 8 9 10 11 12 | with Ada.Text_IO; use Ada.Text_IO; procedure Main is A, B : Integer; R : Float; begin A := 0; B := 1; R := A + B; Put_Line ("Result : " & Float'Image (R)); end Main; |
1 2 3 4 | gprbuild -q -P learn_ada.gpr check_positive.adb:9:11: error: expected type "Standard.Float" check_positive.adb:9:11: error: found type "Standard.Integer" |
[ Logical Comment ]
Boolean
– represents the logically recognized valuesTRUE
orFALSE
.
[ 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
.
1 2 3 4 5 6 7 8 9 10 11 | with Ada.Text_IO; use Ada.Text_IO; procedure Main is -- khai báo biến có độ rộng 10 ký tự Message : String (1 .. 10); begin -- thử lưu chuỗi có độ rộng 6 ký tự Message := "Hello!"; Put_Line (Message); end Main; |
1 2 3 4 5 6 7 | gprbuild -q -P learn_ada.gpr main.adb:7:15: warning: wrong length for array of subtype of "Standard.String" defined at line 5 [enabled by default] main.adb:7:15: warning: Constraint_Error will be raised at run time [enabled by default] out/main raised CONSTRAINT_ERROR : main.adb:7 length check failed |
[ 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 | with Ada.Text_IO; use Ada.Text_IO; procedure Main is subtype Positive_Integer is Integer range 1 .. Integer'Last; P : Positive_Integer; I : Integer; begin I := 9; P := I - 8; P := I - 1; -- error Put_Line ("P is " & Integer'Image (P)); end Main; |
1 2 3 4 5 6 7 | gprbuild -q -P learn_ada.gpr main.adb:10:11: warning: value not in range of type "Positive_Integer" defined at line 4 [enabled by default] main.adb:10:11: warning: Constraint_Error will be raised at run time [enabled by default] out/main raised CONSTRAINT_ERROR : main.adb:10 range check failed |
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
.
1 2 3 4 5 6 7 8 9 10 11 12 | with Ada.Text_IO; use Ada.Text_IO; procedure Main is N : Integer; begin N := 0; Put_Line ("N is " & Integer'Image (N)); -- update value N := N + 1; Put_Line ("Now is " & Integer'Image (N)); end Main; |
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 aValue
value of typeType
to aString
.Type'Value (String)
– converts aString
string to aValue
of typeType
.Type'First
– returns the first value in the range of typeType
.Type'Last
– returns the last value in the range of typeType
.
Here is the complete list of attribute attribute
that Ada
provides: Wikipedia - Attribute List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | with Ada.Text_IO; use Ada.Text_IO; procedure Main is N : Integer; begin N := Integer'Value ("1990"); Put_Line ("Scanned : " & Integer'Image (N)); -- N := Integer'First; Put_Line ("Min : " & Integer'Image (N)); -- N := Integer'Last; Put_Line ("Max : " & Integer'Image (N)); end Main; |
1 2 3 4 | Scanned : 1990 Min : -2147483648 Max : 2147483647 |
In the next article, we will talk about defining new data types ourselves.
(unpublished) [Procedural Programming + Ada] Lesson 6 – Arrays & Records