summaryrefslogtreecommitdiff
path: root/more-types
diff options
context:
space:
mode:
Diffstat (limited to 'more-types')
-rw-r--r--more-types/src/types.adb116
-rw-r--r--more-types/types.gpr5
2 files changed, 121 insertions, 0 deletions
diff --git a/more-types/src/types.adb b/more-types/src/types.adb
new file mode 100644
index 0000000..4e7590f
--- /dev/null
+++ b/more-types/src/types.adb
@@ -0,0 +1,116 @@
1with Ada.Text_IO; use Ada.Text_IO;
2
3procedure Types is
4
5 -----------------------------------------------------------------------------
6 -- Record initialization.
7 -----------------------------------------------------------------------------
8 procedure Test_Point is
9 type Point is record
10 X : Integer := 0;
11 Y : Integer := 0;
12 end record;
13
14 Origin_1 : Point; -- Default initialization.
15 Origin_2 : Point := (0, 0); -- Explicit, unnamed.
16 Origin_3 : Point := (X => 0, Y => 0); -- Explicit, named.
17 Origin_4 : Point := (X => <>, Y => <>); -- Explicit, using defaults.
18 Origin_5 : Point := (X | Y => 0); -- Initialize both values.
19 Origin_6 : Point := Point'(0,0); -- Qualified expression.
20 begin
21 Put_Line ("Origin: " & Integer'Image (Origin_5.X) & ", "
22 & Integer'Image (Origin_5.Y));
23 end Test_Point;
24
25 -----------------------------------------------------------------------------
26 -- Pointers.
27 -----------------------------------------------------------------------------
28 procedure Test_Pointer is
29 type Month_Type is (Jan, Feb, Mar, Apr, May, Jun,
30 Jul, Aug, Sep, Oct, Nov, Dec);
31
32 type Date is record
33 Day : Integer range 1 .. 31;
34 Month : Month_Type;
35 Year : Integer;
36 end record;
37
38 -- Access types are nominally typed, not structurally typed.
39 -- If we "own" a type X, we typically also declare an access type named
40 -- X_Acc, so that there is a canonical name for the access type to X.
41 type Date_Acc is access Date; -- Pointer to Date type.
42 type Different_Date_Acc is access Date; -- Different type.
43
44 Null_Date : Date_Acc := null;
45
46 -- Allocate values of the access type using the 'new' keyword.
47 D : Date_Acc := new Date;
48
49 -- Constraints can be given when instantiating the type.
50 Buffer : access String := new String(1 .. 5);
51
52 -- We can also initialize along with the allocation.
53 Hello_Str : access String := new String'("Hello");
54
55 procedure Test_Null (D : Date_Acc; Name : String) is
56 begin
57 -- Dereferencing of D happens implicitly. Here we can treat D as an
58 -- actual Date.
59 if D = null then
60 Put_Line (Name & " is null");
61 else
62 Put_Line (Name & " is not null");
63 end if;
64 end Test_Null;
65
66 begin
67 Test_Null (Null_Date, "Null_Date");
68 Test_Null (D, "D");
69 end Test_Pointer;
70
71 -----------------------------------------------------------------------------
72 -- Mutually recursive types.
73 --
74 -- Similar to C++, we can forward-declare a type to break the loop.
75 -----------------------------------------------------------------------------
76
77 procedure Test_MyList is
78 type MyList;
79 type MyList_Acc is access MyList;
80
81 type MyList is record
82 Value : Integer := 0;
83 Next : MyList_Acc := null;
84 end record;
85
86 function Cons (X : Integer; L : MyList_Acc) return MyList_Acc is
87 Head : MyList_Acc := new MyList;
88 begin
89 Head.Value := X;
90 Head.Next := L;
91 return Head;
92 end Cons;
93
94 procedure Print_List (L : access constant MyList) is
95 Node : access constant MyList := L;
96 begin
97 Put ("[");
98 while Node /= null loop
99 Put (Integer'Image (Node.Value) & " ");
100 Node := Node.next;
101 end loop;
102 Put_Line ("]");
103 end Print_List;
104
105 InitialList : MyList_Acc := new MyList'(4, null);
106 ModifiedList : MyList_Acc;
107 begin
108 ModifiedList := Cons (1, Cons (2, Cons (3, InitialList)));
109 Print_List (ModifiedList);
110 end Test_MyList;
111
112begin
113 Test_Point;
114 Test_Pointer;
115 Test_MyList;
116end Types;
diff --git a/more-types/types.gpr b/more-types/types.gpr
new file mode 100644
index 0000000..740bcfd
--- /dev/null
+++ b/more-types/types.gpr
@@ -0,0 +1,5 @@
1project Types is
2 for Source_Dirs use ("src");
3 for Object_Dir use "obj";
4 for Main use ("types.adb");
5end Types;