Generics - Introduction
Generics - Introduction
Generics - Nutshell
- Generics provide the ability to create specialized versions of things
- Typically specialized for specific types
Generics - Context: Java
- Java has generic types
- ie generic classes and interfaces
- Example:
ArrayList< Integer> a = new ArrayList< Integer>();
- LHS is a type: ArrayList of Integers, only
- RHS is a instance of ArrayList of Integers, only
Generics - Parameterized
- Class ArrayList is parameterized (ie has a parameter)
- Allows for creation of specialized versions
- A specialized version for the specified type
Generics in Ada
- Ada allows creation and use of generic
- Procedures
- Functions
- Packages
- Each can be parameterized with types, ...
- and values, and packages, and ...
- We will look first at using generic procedures and packages
- Then we will look at creating generic packages and (briefly) procedures
- Examples of use that we will look at:
- Generic procedure:
Ada.Containers.Generic_Array_Sort
- Generic procedure:
Ada.Containers.Generic_Constrained_Array_Sort
- Generic functions:
Ada.Unchecked_Conversion
- later
- Generic packages:
Ada.Text_IO.Enumeration_IO
- Enumeration Types
Generic Sorts
Generic Array Sort
type Array_Type is array(Natural range <>) of Integer;
procedure sort is
Package Ada.Containers
contains generic procedure Generic_Array_Sort
To use this we must provide parameters:
- ItemType
- Index_Type
- Array_Type
- "<"
Generics - Parameterized
Generics - Parameterized
Generics - Parameterized
Generics - Parameterized
Generics - Benefits
- Write code once
- Benefits of strong typing
Generics - History
- Ada 83 - Generics too complicated!
- Java (1995) - by 2000: we need generics!
- Java 1.5 (2005): (renamed Java 5) added generics!