Overloading Sub-programs & Generic Types

Tram Ho

In particular, about the feature of Polymorphism , this is not a unique feature of OOP but one of the common general concepts of the field of computer science in general. And expressing Polymorphism is taken care of in all programming environments including Procedural Programming and Functional Programming .

In an earlier article of the Self- Taught Web Programming Series Naturally , I tried to introduce the general definition and logic types that represent Polymorphism when it comes to the OOP programming paradigm. Among the logical types representing Polymorphism synthesized by Wikipedia , here we have seen Sub-Typing Polymorphism appear with the concept of Type'Class .

In addition, the remaining representation logic types are Ad-hoc Polymorphism and Parametric Polymorphism supported by other features of the language, including:

  • Overloading Sub-program – redefine different versions of the same sub-program name with different signature sets, including parameter data types and return data types (if any) .
  • Generic Type – uses the abstract symbolic name Type for the type operation to represent any data type used as parameters to sub-program and package . Then, at each time we write code using sub-program and package , we can specify a specific data type at the place of Type depending on our needs.

Overloading Sub-programs

Here we will do a small example encapsulating the main.adb launch file with the procedure name Put_Total which will be overload with the signature respectively: Put_Total (String, String) and Put_Total (Integer, Integer) .

overloading function is no different from procedure , as long as we create different signature for each version of the function definition of the same name. The only funtion is that for the function, we will have signature including the return data type. And we probably won’t need to write more example code for funtion .

Generic Sub-programs

Let’s start with a simple example, defining the generic type used for a sub-program .

So we have type T defined as a generic type and used to type the parameter Value . Now when writing code using Print , we can specify a specific data type depending on the intended use.

Note about the GNAT compiler – To use the Image property of any type T , we will need to specify the compiler mode of operation using the latest version of the Ada language .

Great.. So we didn’t have to repeat the code that defines the Print program for each data type. And in case we need to use more generic parameters, we just need to define more generic type similar to T .

Another note, is that when using generic sub-program as above, we don’t necessarily need to give different names to specific sub-program , but can be written in overloading form with the same name.

Generic Package

In case we want to use generic type in the definition of record , we will need to move the declaration of generic type outside the package . This will indicate that – this package is designed to be generic.

Here is the example code of the access pointer article we went through earlier that is rewritten with generic type .

And when writing code using generic package , we also use the new syntax to specify a specific data type for all locations using T in the definition of the package .

In the next article, we will learn about a very special tool of Ada – present in a handful of other programming languages.

Share the news now

Source : Viblo