Can't understand how generics work - ada

I have a package called Linked_List(.ads) Here is the code in it:
Generic
type T is private;
package Linked_List is
type List is tagged record
Data : T;
end record;
end Linked_List;
and here is the code in the package which contains the main function (main.adb)
with Ada.Text_IO; use Ada.Text_IO;
with Linked_List;
procedure Main is
type ListOfIntegers is new Linked_List(T => Integer);
begin
null;
end Main;
I'm getting this error:
4:30 subtype mark required in this context
found "Linked_List" declared at linked_list.ads:3
found "Linked_List" declared at linked_list.ads:3
4:41 incorrect constrain for this kind of type
Any help is appreciated.

new Linked_List(T => Integer) defines a new package, not a new type. The error messages you’re getting are because the compiler thinks you’re declaring a type, so seeing the name of a package at column 30 confused it; it wanted to see the name of a (sub)type.
Line 4 should read
package ListOfIntegers is new Linked_List(T => Integer);
after which there is a type ListOfIntegers.List, so you can write
My_List : ListOfIntegers.List;
You may find having to say ListOfIntegers. all the time annoying; you can say
use ListOfIntegers;
after which you can just write
My_List : List;
but it’s usually thought best not to overdo this (if you have dozens of “withed” packages, “using” them all makes it difficult to know which one you’re referring to).
By the way, normal Ada usage is to use underscores to separate words in identifiers: List_Of_Names.

Related

Deep copy of function arguments for polymorphic types

I use an object based on a base package roughly defined as:
package Base is
type T_Base is abstract tagged null record;
-- This performs a deep copy. Shallow copies may lead to STORAGE_ERROR.
-- This shall be implemented by every derived type.
function Copy (From : in T_Base) return T_Base'Class is abstract;
end package Base;
This package is derived by several packages which are further derived
package Foo is
type T_Foo is new T_Base with record
A_Data : Natural; -- Of course, in the real code, these are types by far more complex.
end record;
procedure do_something (Foo_Object : in T_Foo);
-- This implements the deep copy
function Copy (From : in T_Foo) return T_Base'Class is abstract;
end package Foo;
On calling the procedure do_something, I do get a storage_error:
procedure handle_received_foo (Foo_In: in Foo.T_Foo) is
begin
Foo.do_something (Foo_Object => Foo_In); -- The storage error does happen here.
end main;
When running the code with gdb, I get a segfault on entering the function and I get:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 39 (LWP 39)]
0x033c9828 in foo.do_something (foo_object=...) at ./foo.adb:67
67 procedure do_something (Foo_Object : in T_Foo);
(gdb) p foo_object
$1 (null)
So I guess I get a storage_error when doing the shallow copy of the argument Foo_Object.
I am aware that this is no MWE and that there might be a mistake in one of the types present used in the derived types.
I can't find any good option:
Making T_Foo a Controlled type to call Copy in Adjust seems not to be possible without greatly changing its definition as I can't derive T_Foo both from T_Base and Ada.Finalization.Controlled since none of them is an interface types
Defining T_Base as
type T_Base is abstract new Ada.Finalization.Controlled with null record;
and override Adjust here seems to induce a hell lot too much modifications on the existing code base as gnat yields in multiple places
type of aggregate has private ancestor "Controlled" must use extension aggregate.
So I'm low on solutions to either investigate the problem further or to solve it with a hammer.
The problem was not in the Copy function. The comments I saw in the code base were misleading.
The fact that introducing new variables changed the location of the exception made me consider some stack overflow problems.
Indeed the Storage_Size allocated for the task was not sufficient. Increasing the pragma Storage_Size(<value>) solved the problem.
Since the code base is compiled with -fstack-check, this led to the aforementioned STORAGE_ERROR.
More infos on Adacore's documentation.
This could probably have been seen with Gem #95: Dynamic Stack Analysis in GNAT but I am not currently able to see any result with the binding option suggested therein.

How do you implement Generic_Sorting in Ada for a vector?

I'm trying to do some basic translations of old C++ code from many moons ago to learn Ada, and I have been absolutely stumped on how to sort a vector using the built-in Generic_Sorting. I haven't been able to find any concrete examples of it in action, the closest being a now-defunct Danish wiki article which looks like it would have had a full example but the archive didn't snatch it up: https://web.archive.org/web/20100222010428/http://wiki.ada-dk.org/index.php/Ada.Containers.Vectors#Vectors.Generic_Sorting
Here is the code as I thought it should work from the above link:
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Containers.Vectors; use Ada.Containers;
procedure Vectortest is
package IntegerVector is new Vectors
(Index_Type => Natural,
Element_Type => Integer);
package IVSorter is new Generic_Sorting;
IntVec : IntegerVector.Vector;
Cursor : IntegerVector.Cursor;
begin
IntVec.Append(3);
IntVec.Append(43);
IntVec.Append(34);
IntVec.Append(8);
IVSorter.Sort(Container => IntVec);
Cursor := IntegerVector.First(Input);
while IntegerVector.Has_Element(Cursor) loop
Put(IntegerVector.Element(Cursor));
IntegerVector.Next(Cursor);
end loop;
end Vectortest;
I've tried so many different combinations of use and with but all I can get are various error codes. The above code gives Generic_Sorting is not visible, but when I try to explicitly state with Ada.Containers.Vectors.Generic_Sorting I get the error "Ada.Containers.Vectors.Generic_Sorting" is not a predefined library unit. I have no idea what I'm doing wrong here, I'm sure it's a fundamental misunderstanding of the way Ada brings in packages, and I hope that nailing this down will help me understand it a bit better.
Generic_Sorting is an inner package to Ada.Containers.Vectors and cannot be explicitly withed (as you have discovered). And since Ada.Containers.Vectors is itself a generic package, Generic_Sorting is an inner package to the instantiation of Ada.Containers.Vectors. So you can reach it by prefixing with the name of the instance:
package IVSorter is new IntegerVector.Generic_Sorting;

Constant element in Ada object?

In Java or C#, you would often have class members that are final or readonly - they are set once and then never touched again. They can hold different values for different instances of the class.
Is there something similar in Ada? I've tried to create something similar in Ada thusly:
package MyPackage is
type MyObject is limited new OtherPackage.Object with private;
....
private
type MyObject (...) is limited new OtherPackage.Object with
record
M_MyField : Integer := 10;
M_MyConstantFactory : constant Factory.Object'Class := new Factory.Object;
end record;
end MyPackage;
This fails on the declaration of M_MyConstantFactory saying constant components are not permitted. Is there a way around this? A colleague suggested declaring it somewhere else in the package, but that would mean a single M_MyConstantFactory shared across all instances, which is not what I want.
Do I need to just accept that it is possible to modify the value once set and manually guard against that happening?
No. Not quite.
If your component is of a discrete type or an access type, you can make it a discriminant, and thus make it immutable.
with Ada.Integer_Text_IO;
procedure Immutable_Components is
type Instance (Immutable : Positive) is null record;
A : Instance := (Immutable => 1);
begin
Ada.Integer_Text_IO.Put (A.Immutable);
-- A.Immutable := 2; -- assignment to discriminant not allowed:
end Immutable_Components;
Before I answer the question, it would probably be helpful to distinguish between Ada and Java/C#'s modeling of objects. In Java, everything is an object, and so all constants have to be final -- in Ada things are a bit different, Ada's object-system ("tagged types" in Ada parlance) is built upon two items: records and type-derivation. This means that, when teaching OOP, we could arrive gradually introducing first type-derivation (eg Type Degree is new Integer;), then records (ie encapsulation), then private-types (ie information-hiding), and finally unifying everything together with tagged-types... all of which I'll assume you are cognizant of.
In Ada, a constant is just that: some object that can be read but [generally] not written to. (Things can get funny with eg memory-mapped IO.) So we could say:
Package Ex1 is
Type Stub1 is private; -- Some type, with almost nothing public.
C1 : Constant Stub1; -- A constant of that type.
Private
Type Stub1 is tagged record
Data_1 : Integer;
Data_2 : Float;
end;
-- And now we can tell the compiler what C1 _is_.
C1: Constant Stub1 := (Data_1 => 3, Data_2 => 1.2);
End Ex1;
That's how we'd make a constant for a tagged type while keeping its implementation details hidden; though, admittedly, we could have exposed everything and gotten rid of the entire private section.
Now we get to an interesting feature of records [and tagged types] called discriminants -- these are kind of like constants, and kind of like generic-types in other languages. With discriminants we could make a message-type that varies in size according to the message-length:
Package Ex2 is
Type Message(Length : Natural) is private; -- A message.
Function Create( Text : String ) return Message;
Private
Type Message(Length : Natural) is record
Data : String(1..Length) := (Others => ' '); -- Defaults to space-filled string.
end;
Function Create( Text : String ) return Message is
( Data => Text, Length => Text'Length );
End Ex2;
Now, in this case, when you do an assignment like X : Message := Create("Steve"); the variavle's type [unconstrained, in this example, becomes constrained in this case to Message(5) (because "Steve" is 5 characters) and so trying to re-assign with a different-sized message-string wouldn't work. (So, while you couldn't say X:= Create("Why") you can say X:= Create("Hello") because the discriminant [Length] here is 5.) -- So, in that manner discriminants can act like constant fields in some cases.
The limited keyword means that the type doesn't have an assignment [but does have initialization], so you could make the entire type behave as a constant; this is different than having the one component be constant though, certainly not as subtle as the distinction between T and T'Class (T'Class is the Type T and all types derived therefrom, where as T is only that type.)
You almost have the general solution (for cases when it can't be a discriminant) already. The type is limited private. Clients of the package can only modify it through the operations the pkg provides. As long as the operations don't modify the field in question, you have what you want (unless I misunderstood and the question is how to prevent yourself from modifying the field in the pkg body).

What is "Subtype mark required in this context" exactly?

I get Subtype mark required in this context at (*). What exactly is subtype mask and why is it complaining here?
main.adb
(*)Open_Route : Route(1..3) := (others => new Location(X=>1.0,Y=>1.0, id=>1));
-- Closed_Route : Route (Open_Route'First .. Open_Route'Last + 1);
-- P1 : Population (1..2);
Location.ads package spec
type Location is record
Id : Positive;
X : Float;
Y : Float;
end record;
type Location_Acess is access all Location;
type Route is array (Positive range<>) of Location_Acess;
type Route_Acess is access all Route;
type Population is array (Positive range<>) of Route_Acess;
A subtype_mark is basically a name that denotes a type or subtype (ARM 3.2.2(4)). The reason why the ARM uses subtype_mark throughout is to do with some arcane distinction (ARM 3.2.1); when you say type Foo is you are declaring both a type and its first subtype, and the name Foo refers to the first subtype.
I think your problem is that you have a package Location containing an entity of the same name, and that this has confused the compiler.
You haven’t supplied a complete compilable code example, but reading between the lines I think it’d be something like this, after applying #ajb’s suggestion:
package Location is
type Location is record
Id : Positive;
X : Float;
Y : Float;
end record;
type Location_Acess is access all Location;
type Route is array (Positive range<>) of Location_Acess;
type Route_Acess is access all Route;
type Population is array (Positive range<>) of Route_Acess;
end Location;
with Location; use Location;
package Location_User is
Open_Route : Route(1..3) := (others => new Location'(X=>1.0,Y=>1.0, id=>1));
end Location_User;
and the compiler error message would be
location_user.ads:3:47: subtype mark required in this context
location_user.ads:3:47: found "Location" declared at location.ads:1
(see how it’s telling you to look at line 1 of location.ads, the package name?)
You could work round this by saying new Location.Location, but then you’d need to say it everywhere.
There are two canonical solutions, and there is a religious divide between the supporters of the two camps.
The first (my preferred one) would be to call the package Locations and leave the types Location, Route, Population as they are.
The second (which I consider ugly and would only use in dire need) would be to decorate type names with a suffix:
type Location_Type is
or
type Location_T is
The source code given in the question is not complete (you did not state whether you used or merely withed the Location package. I assume the latter since (at least with the compiler I have installed) the error message given is invalid constraint: type has no discriminant.
#ajb has already pointed out the solution to that problem: without the quote mark, the compiler looks for a type with a discriminant (a type parameterised by another type), but Location does not have a discriminant.
Moreover, unless you used the Location package, new Location does not actually refer to a type (or subtype), but to a package. If you do not want to use Location, type
new Location.Location'(… here. One way to avoid such errors (and get better error messages) is to use different names for packages and type: when both have the same name, it is not always intuitively clear how the compiler interprets the occurrence of the name, and when the opinions of compiler and programmer differ, confusion ensues.
Personally, I like to use the plural for for the package, so in this case, there would be a package Locations containing the type Location, but there are other possibilities.

How to print integers in ada83 environment

I want to print integers in Ada 83. At present I am just using 'with Text_IO' and 'use Text_IO'. I don't want to print using the Integer'Image option. I want to use Integer_Text_IO in ada83. Please help me out with the syntax.
I am using below code:
with Text_IO;
use Text_IO;
i: INTEGER :=1;
package Int_IO is new Integer_IO(INTEGER);
use Int_IO; put(i);
I am getting 'expect signed integer type in instantiation of "Num" ' error.
The example below, which compiles, should help.
But please, when posting a question on StackOverflow (or anywhere on the Net, really) show us the code you’ve actually tried. The sample you’ve provided doesn’t come close to compiling (it fails at line 3 with compilation unit expected), and that makes it very hard for us to work out how to help you.
You’ll get expect signed integer type in instantiation of “Num” if you try to instantiate Text_IO with the wrong sort of type (for example, Float).
with Text_IO;
procedure Integer_IO_Demo is
package Int_IO is new Text_IO.Integer_IO (Integer);
begin
for J in 5 .. 10 loop
Int_IO.Put (J);
Text_IO.New_Line;
end loop;
end Integer_IO_Demo;

Resources