Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a tagged type in Ada?

Tags:

ada

Currently learning Ada and actually enjoying it, there is a something that bothers me: what's a tagged type? According to Programming in Ada 2012 by John Barnes, it indicates that the instantiated object carries a tag at run time.

I never heard of anything like this in C++ or C I think, so I'm a bit lost. What is it? When do I need it (apparently for having methods and inheritance?)?

like image 843
Louis Etienne Avatar asked Aug 31 '21 06:08

Louis Etienne


People also ask

What is a tagged record in Ada?

The tagged record is one part of what in other languages is called a class. It is the basic foundation of object orientated programming in Ada. The other two parts a class in Ada needs is a package and primitive operations . type Person is tagged record Name : String (1 .. 10); Gender : Gender_Type; end record ;

What are subprograms in Ada?

They are the equivalent of C++ member functions and Java instance methods. While in C++ and Java these subprograms are located within the nested scope of the type, in Ada they are simply declared in the same scope as the type. There's no syntactic indication that a subprogram is a primitive of a type.

What is the difference between root and root class Ada?

Unlike many other OOP languages, Ada differentiates between a reference to a specific tagged type, and a reference to an entire tagged type hierarchy. While Root is used to mean a specific type, Root'Class — a class-wide type — refers to either that type or any of its descendants.

What does the tag of a specific tagged type mean?

The tag of a specific tagged type identifies the full_type_declaration of the type, and for a type extension, is sufficient to uniquely identify the type among all descendants of the same ancestor.


1 Answers

It is simply a class. That's a way in Ada to declare the root of a class hierarchy. Another way is to use interfaces.

It is also, at the moment, the way to obtain dot notation for a type (but this will be generalized in Ada 2022).

See https://learn.adacore.com/courses/Ada_For_The_CPP_Java_Developer/chapters/08_Classes_and_Object_Oriented_Programming.html

So you rarely if ever directly manipulate the tag, just the same as vtables are behind the scenes providing the dispatching but you don't need to think about them in C++.

A notable difference with these languages is that T'Class can be used to refer to a whole family of derived types, and it must be used explicitly to achieve dynamic dispatch.

like image 138
Álex Avatar answered Nov 07 '22 00:11

Álex