SlideShare a Scribd company logo
1 of 19
Download to read offline
Franco Gasperoni
                             gasperon@act-europe.fr
                             http://libre.act-europe.fr

                                                                                           1
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
Copyright Notice

           • © ACT Europe under the GNU Free Documentation License

           • Permission is granted to copy, distribute and/or modify this
             document under the terms of the GNU Free Documentation
             License, Version 1.1 or any later version published by the Free
             Software Foundation; provided its original author is mentioned
             and the link to http://libre.act-europe.fr/ is kept at the bottom of
             every non-title slide. A copy of the license is available at:
           •                          http://www.fsf.org/licenses/fdl.html




                                                                                            2
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
3
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
We often write similar code...
      procedure Swap (X, Y ::in out Integer) is
      procedure Swap (X, Y in out Integer is
                                     Integer)
         Tmp ::Integer := X;
          Tmp Integer := X;
      begin
      begin
          X := Y;
          X := Y;
          Y := Tmp;
          Y := Tmp;
      end
      end

                             procedure Swap (X, Y ::in out Float) is
                             procedure Swap (X, Y in out Float is
                                                           Float)
                                Tmp :: Float := X;
                                 Tmp Float := X;
                             begin
                             begin
                                 X := Y;
                                 X := Y;
                                 Y := Tmp;
                                 Y := Tmp;
                             end
                             end
                                                                                       4
http://libre.act-europe.fr     © ACT Europe under the GNU Free Documentation License
This is...

           • Time consuming

           • Error prone (cut & paste)

           • Maintenance hazard




                                                                                     5
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
Genericity allows you to
                        parameterize your code
                                                                      Any non limited type
   gen_swap.ads                                                       without discriminants
     generic
     generic
         type Some_Type is private;
        type Some_Type is private;
     procedure Gen_Swap (X, Y ::in out Some_Type);
     procedure Gen_Swap (X, Y in out Some_Type
                                       Some_Type);


   gen_swap.adb
     procedure Gen_Swap (X, Y ::in out Some_Type) is
      procedure Gen_Swap (X, Y in out Some_Type is
                                       Some_Type)
         Tmp ::Some_Type := X;
         Tmp Some_Type := X;
      begin
     begin
          X := Y;
         X := Y;
          Y := Tmp;
         Y := Tmp;
      end Gen_Swap;
     end Gen_Swap;                                                                            6
http://libre.act-europe.fr     © ACT Europe under the GNU Free Documentation License
Instantiating a Generic
  with Gen_Swap;
  with Gen_Swap;                                                               Generic instantiation
  procedure Client is
  procedure Client is

      procedure Swap is new Gen_Swap (Some_Type => Integer);
      procedure Swap is new Gen_Swap (Some_Type => Integer);
      procedure Swap is new Gen_Swap (Some_Type => Float);
      procedure Swap is new Gen_Swap (Some_Type => Float);

      A, B ::Integer := …;
      A, B Integer := …;
      P, Q ::Float := …;
      P, Q Float := …;

  begin
  begin
    Swap (A, B);
   Swap (A, B);
    Swap (P, Q);
   Swap (P, Q);
  end Swap;
  end Swap;
                                                                                                   7
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
Type of Generic Units
       generic
       generic
           … formal parameters ...
          … formal parameters ...
       procedure Proc (…);
       procedure Proc (…);

                                                  generic
                                                 generic
                                                     … formal parameters ...
                                                     … formal parameters ...
                                                  function Func (…) return …;
                                                 function Func (…) return …;

       generic
       generic
          … formal parameters ...
          … formal parameters ...
       package Pack is
       package Pack is
          …
          …
       end Pack;
       end Pack;                                                                          8
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
… formal parameters ...

           • Types

           • Objects

           • Subprograms

           • Packages


                                                                                           9
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
Another example

           Write a generic function that computes
                                                                    H
                             Sum( L, H ) = ∑ F (i )
                                                                  i=L
           where
             L & H are integer bounds
             F is some function returning some type
                  An addition operation is available for this type

                                                                                           10
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
Spec ...

         generic
         generic
            type Res_Type is private;
           type Res_Type is private;

                Zero ::in Res_Type;
                Zero in Res_Type;

                with function Add (X, Y ::Res_Type) return Res_Type;
                with function Add (X, Y Res_Type) return Res_Type;
                with function F (I ::Integer)
                with function F (I Integer)          return Res_Type;
                                                    return Res_Type;

         function Sum (L, H ::Integer) return Res_Type;
          function Sum (L, H Integer) return Res_Type;




                                                                                        11
http://libre.act-europe.fr      © ACT Europe under the GNU Free Documentation License
Body ...

         function Sum (L, H ::Integer) return Res_Type is
          function Sum (L, H Integer) return Res_Type is
            Result ::Res_Type := Zero;
             Result Res_Type := Zero;

         begin
         begin
           for IIin L .. H loop
            for in L .. H loop
               Result := Add (Result, F (I));
              Result := Add (Result, F (I));
            end loop;
           end loop;

            return Result;
           return Result;
         end Sum;
         end Sum;


                                                                                      12
http://libre.act-europe.fr    © ACT Europe under the GNU Free Documentation License
Instantiating Sum
     with Sum;
     with Sum;
     procedure Client is
     procedure Client is
        function Compute (X ::Integer) return Integer is … end;
         function Compute (X Integer) return Integer is … end;
        function Compute (X ::Integer) return Float is … end;
         function Compute (X Integer) return Float is … end;

            function New_Sum is new Sum (Res_Type => Integer,
           function New_Sum is new Sum (Res_Type => Integer,
                                         Zero     => 0,
                                         Zero    => 0,
                                         Add      => “+”,
                                         Add     => “+”,
                                         F        => Compute);
                                         F       => Compute);

            function New_Sum is new Sum (Res_Type => Float,
           function New_Sum is new Sum (Res_Type => Float,
                                         Zero     => 0.0,
                                         Zero    => 0.0,
                                         Add      => “+”,
                                         Add     => “+”,
                                         F        => Compute);
                                         F       => Compute);
                                                                                        13
http://libre.act-europe.fr      © ACT Europe under the GNU Free Documentation License
Default Generic Parameters

  generic
  generic
    type Res_Type is private;
   type Res_Type is private;

      Zero ::in Res_Type;
      Zero in Res_Type;

      with function Add (X, Y ::Res_Type) return Res_Type;
      with function Add (X, Y Res_Type) return Res_Type;
                                           return Res_Type is <>;
      with function F (I ::Integer)
      with function F (I Integer)         return Res_Type is <>;

  function Sum (L, H ::Integer) return Res_Type;
   function Sum (L, H Integer) return Res_Type;



                                                                                     14
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
You can omit F => F
     with Sum;
     with Sum;
     procedure Client is
     procedure Client is
        function F (X ::Integer) return Integer is … end;
         function F (X Integer) return Integer is … end;
        function F (X ::Integer) return Float is … end;
         function F (X Integer) return Float is … end;

            function New_Sum is new Sum (Res_Type => Integer,
           function New_Sum is new Sum (Res_Type => Integer,
                                         Zero     => 0,
                                         Zero    => 0,
                                         Add      => “+”);
                                         Add     => “+”);

            function New_Sum is new Sum (Res_Type => Float,
           function New_Sum is new Sum (Res_Type => Float,
                                         Zero     => 0.0,
                                         Zero    => 0.0,
                                         Add      => “+”);
                                         Add     => “+”);

              You can omit parameter F if there is a visible
              routine called F with the right parameters                                  15
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
Allowed to use
                              “+” “-” “*” etc.
                             as function names
   package Sets is
   package Sets is
       type Set is private;
      type Set is private;

       function “+” (S1, S2 ::Set) return Set;
        function “+” (S1, S2 Set) return Set;                                              -- Set union
                                                                                          -- Set union
       function “*” (S1, S2 ::Set) return Set;
        function “*” (S1, S2 Set) return Set;                                             -- Set intersection
                                                                                         -- Set intersection
       function “-” (S1, S2 ::Set) return Set;
        function “-” (S1, S2 Set) return Set;                                              -- Set difference
                                                                                          -- Set difference
        …
       …
    private
    private
        type Set is …;
       type Set is …;
   end Alerts;
   end Alerts;
                                                                                                           16
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License
Back to our Generic Sum


   generic
   generic
     type Res_Type is private;
    type Res_Type is private;

       Zero ::in Res_Type;
       Zero in Res_Type;

       with function “+” (X, Y :: Res_Type) return Res_Type is <>;
       with function “+” (X, Y Res_Type) return Res_Type is <>;
                                             return Res_Type is <>;
       with function F (I ::Integer)
       with function F (I Integer)          return Res_Type is <>;

   function Sum (L, H ::Integer) return Res_Type;
    function Sum (L, H Integer) return Res_Type;
                                                                                     17
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
Body ...

         function Sum (L, H ::Integer) return Res_Type is
          function Sum (L, H Integer) return Res_Type is
            Result ::Res_Type := Zero;
             Result Res_Type := Zero;

         begin
         begin
           for IIin L .. H loop
            for in L .. H loop
               Result := Result + F (I);
              Result := Result + F (I);
            end loop;
           end loop;

            return Result;
           return Result;
         end Sum;
         end Sum;


                                                                                      18
http://libre.act-europe.fr    © ACT Europe under the GNU Free Documentation License
You can omit
                             F => F and “+” => “+”
     with Sum;
     with Sum;
     procedure Client is
     procedure Client is
        function F (X ::Integer) return Integer is … end;
         function F (X Integer) return Integer is … end;
        function F (X ::Integer) return Float is … end;
         function F (X Integer) return Float is … end;

            function New_Sum is new Sum (Res_Type => Integer,
           function New_Sum is new Sum (Res_Type => Integer,
                                         Zero     => 0);
                                         Zero    => 0);

            function New_Sum is new Sum (Res_Type => Float,
           function New_Sum is new Sum (Res_Type => Float,
                                         Zero     => 0.0);
                                         Zero    => 0.0);


                                                                                           19
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License

More Related Content

More from Gneuromante canalada.org

More from Gneuromante canalada.org (8)

Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs
Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada ProgramsAst2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs
Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs
 
SIGAda Hibachi Workshop Presentation
SIGAda Hibachi Workshop PresentationSIGAda Hibachi Workshop Presentation
SIGAda Hibachi Workshop Presentation
 
Developing Software that Matters (condensed)
Developing Software that Matters (condensed)Developing Software that Matters (condensed)
Developing Software that Matters (condensed)
 
Ada at Barco avionics
Ada at Barco avionicsAda at Barco avionics
Ada at Barco avionics
 
Programming Languages and Software Construction
Programming Languages and Software ConstructionProgramming Languages and Software Construction
Programming Languages and Software Construction
 
Ada 95 - Distributed systems
Ada 95 - Distributed systemsAda 95 - Distributed systems
Ada 95 - Distributed systems
 
Developing Software That Matters I
Developing Software That Matters IDeveloping Software That Matters I
Developing Software That Matters I
 
Ada in Debian GNU/Linux
Ada in Debian GNU/LinuxAda in Debian GNU/Linux
Ada in Debian GNU/Linux
 

Recently uploaded

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 

Recently uploaded (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 

Generic Programming in Ada

  • 1. Franco Gasperoni gasperon@act-europe.fr http://libre.act-europe.fr 1 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 2. Copyright Notice • © ACT Europe under the GNU Free Documentation License • Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; provided its original author is mentioned and the link to http://libre.act-europe.fr/ is kept at the bottom of every non-title slide. A copy of the license is available at: • http://www.fsf.org/licenses/fdl.html 2 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 3. 3 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 4. We often write similar code... procedure Swap (X, Y ::in out Integer) is procedure Swap (X, Y in out Integer is Integer) Tmp ::Integer := X; Tmp Integer := X; begin begin X := Y; X := Y; Y := Tmp; Y := Tmp; end end procedure Swap (X, Y ::in out Float) is procedure Swap (X, Y in out Float is Float) Tmp :: Float := X; Tmp Float := X; begin begin X := Y; X := Y; Y := Tmp; Y := Tmp; end end 4 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 5. This is... • Time consuming • Error prone (cut & paste) • Maintenance hazard 5 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 6. Genericity allows you to parameterize your code Any non limited type gen_swap.ads without discriminants generic generic type Some_Type is private; type Some_Type is private; procedure Gen_Swap (X, Y ::in out Some_Type); procedure Gen_Swap (X, Y in out Some_Type Some_Type); gen_swap.adb procedure Gen_Swap (X, Y ::in out Some_Type) is procedure Gen_Swap (X, Y in out Some_Type is Some_Type) Tmp ::Some_Type := X; Tmp Some_Type := X; begin begin X := Y; X := Y; Y := Tmp; Y := Tmp; end Gen_Swap; end Gen_Swap; 6 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 7. Instantiating a Generic with Gen_Swap; with Gen_Swap; Generic instantiation procedure Client is procedure Client is procedure Swap is new Gen_Swap (Some_Type => Integer); procedure Swap is new Gen_Swap (Some_Type => Integer); procedure Swap is new Gen_Swap (Some_Type => Float); procedure Swap is new Gen_Swap (Some_Type => Float); A, B ::Integer := …; A, B Integer := …; P, Q ::Float := …; P, Q Float := …; begin begin Swap (A, B); Swap (A, B); Swap (P, Q); Swap (P, Q); end Swap; end Swap; 7 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 8. Type of Generic Units generic generic … formal parameters ... … formal parameters ... procedure Proc (…); procedure Proc (…); generic generic … formal parameters ... … formal parameters ... function Func (…) return …; function Func (…) return …; generic generic … formal parameters ... … formal parameters ... package Pack is package Pack is … … end Pack; end Pack; 8 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 9. … formal parameters ... • Types • Objects • Subprograms • Packages 9 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 10. Another example Write a generic function that computes H Sum( L, H ) = ∑ F (i ) i=L where L & H are integer bounds F is some function returning some type An addition operation is available for this type 10 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 11. Spec ... generic generic type Res_Type is private; type Res_Type is private; Zero ::in Res_Type; Zero in Res_Type; with function Add (X, Y ::Res_Type) return Res_Type; with function Add (X, Y Res_Type) return Res_Type; with function F (I ::Integer) with function F (I Integer) return Res_Type; return Res_Type; function Sum (L, H ::Integer) return Res_Type; function Sum (L, H Integer) return Res_Type; 11 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 12. Body ... function Sum (L, H ::Integer) return Res_Type is function Sum (L, H Integer) return Res_Type is Result ::Res_Type := Zero; Result Res_Type := Zero; begin begin for IIin L .. H loop for in L .. H loop Result := Add (Result, F (I)); Result := Add (Result, F (I)); end loop; end loop; return Result; return Result; end Sum; end Sum; 12 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 13. Instantiating Sum with Sum; with Sum; procedure Client is procedure Client is function Compute (X ::Integer) return Integer is … end; function Compute (X Integer) return Integer is … end; function Compute (X ::Integer) return Float is … end; function Compute (X Integer) return Float is … end; function New_Sum is new Sum (Res_Type => Integer, function New_Sum is new Sum (Res_Type => Integer, Zero => 0, Zero => 0, Add => “+”, Add => “+”, F => Compute); F => Compute); function New_Sum is new Sum (Res_Type => Float, function New_Sum is new Sum (Res_Type => Float, Zero => 0.0, Zero => 0.0, Add => “+”, Add => “+”, F => Compute); F => Compute); 13 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 14. Default Generic Parameters generic generic type Res_Type is private; type Res_Type is private; Zero ::in Res_Type; Zero in Res_Type; with function Add (X, Y ::Res_Type) return Res_Type; with function Add (X, Y Res_Type) return Res_Type; return Res_Type is <>; with function F (I ::Integer) with function F (I Integer) return Res_Type is <>; function Sum (L, H ::Integer) return Res_Type; function Sum (L, H Integer) return Res_Type; 14 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 15. You can omit F => F with Sum; with Sum; procedure Client is procedure Client is function F (X ::Integer) return Integer is … end; function F (X Integer) return Integer is … end; function F (X ::Integer) return Float is … end; function F (X Integer) return Float is … end; function New_Sum is new Sum (Res_Type => Integer, function New_Sum is new Sum (Res_Type => Integer, Zero => 0, Zero => 0, Add => “+”); Add => “+”); function New_Sum is new Sum (Res_Type => Float, function New_Sum is new Sum (Res_Type => Float, Zero => 0.0, Zero => 0.0, Add => “+”); Add => “+”); You can omit parameter F if there is a visible routine called F with the right parameters 15 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 16. Allowed to use “+” “-” “*” etc. as function names package Sets is package Sets is type Set is private; type Set is private; function “+” (S1, S2 ::Set) return Set; function “+” (S1, S2 Set) return Set; -- Set union -- Set union function “*” (S1, S2 ::Set) return Set; function “*” (S1, S2 Set) return Set; -- Set intersection -- Set intersection function “-” (S1, S2 ::Set) return Set; function “-” (S1, S2 Set) return Set; -- Set difference -- Set difference … … private private type Set is …; type Set is …; end Alerts; end Alerts; 16 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 17. Back to our Generic Sum generic generic type Res_Type is private; type Res_Type is private; Zero ::in Res_Type; Zero in Res_Type; with function “+” (X, Y :: Res_Type) return Res_Type is <>; with function “+” (X, Y Res_Type) return Res_Type is <>; return Res_Type is <>; with function F (I ::Integer) with function F (I Integer) return Res_Type is <>; function Sum (L, H ::Integer) return Res_Type; function Sum (L, H Integer) return Res_Type; 17 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 18. Body ... function Sum (L, H ::Integer) return Res_Type is function Sum (L, H Integer) return Res_Type is Result ::Res_Type := Zero; Result Res_Type := Zero; begin begin for IIin L .. H loop for in L .. H loop Result := Result + F (I); Result := Result + F (I); end loop; end loop; return Result; return Result; end Sum; end Sum; 18 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 19. You can omit F => F and “+” => “+” with Sum; with Sum; procedure Client is procedure Client is function F (X ::Integer) return Integer is … end; function F (X Integer) return Integer is … end; function F (X ::Integer) return Float is … end; function F (X Integer) return Float is … end; function New_Sum is new Sum (Res_Type => Integer, function New_Sum is new Sum (Res_Type => Integer, Zero => 0); Zero => 0); function New_Sum is new Sum (Res_Type => Float, function New_Sum is new Sum (Res_Type => Float, Zero => 0.0); Zero => 0.0); 19 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License