Download presentation
Presentation is loading. Please wait.
Published byJulius Matthews Modified over 9 years ago
1
1 1 http://libre.adacore.com © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com http://libre.adacore.com
2
2 2 © AdaCore under the GNU Free Documentation License Copyright Notice © AdaCore 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
3
3 3 http://libre.adacore.com © AdaCore under the GNU Free Documentation License
4
4 4 http://libre.adacore.com © AdaCore under the GNU Free Documentation License We often write similar code... Integer procedure Swap (X, Y : in out Integer) is Integer Tmp : Integer := X; begin X := Y; Y := Tmp; end Integer procedure Swap (X, Y : in out Integer) is Integer Tmp : Integer := X; begin X := Y; Y := Tmp; end Float procedure Swap (X, Y : in out Float) is Float Tmp : Float := X; begin X := Y; Y := Tmp; end Float procedure Swap (X, Y : in out Float) is Float Tmp : Float := X; begin X := Y; Y := Tmp; end
5
5 5 http://libre.adacore.com © AdaCore under the GNU Free Documentation License This is... Time consuming Error prone (cut & paste) Maintenance hazard
6
6 6 http://libre.adacore.com © AdaCore under the GNU Free Documentation License Genericity allows you to parameterize your code generic Some_Type type Some_Type is private; Some_Type procedure Gen_Swap (X, Y : in out Some_Type); generic Some_Type type Some_Type is private; Some_Type procedure Gen_Swap (X, Y : in out Some_Type); Any non limited type without discriminants gen_swap.ads Some_Type procedure Gen_Swap (X, Y : in out Some_Type) is Some_Type Tmp : Some_Type := X; begin X := Y; Y := Tmp; end Gen_Swap; Some_Type procedure Gen_Swap (X, Y : in out Some_Type) is Some_Type Tmp : Some_Type := X; begin X := Y; Y := Tmp; end Gen_Swap; gen_swap.adb
7
7 7 http://libre.adacore.com © AdaCore under the GNU Free Documentation License Instantiating a Generic with Gen_Swap; procedure Client is procedure Swap is new Gen_Swap (Some_Type => Integer); procedure Swap is new Gen_Swap (Some_Type => Float); A, B : Integer := …; P, Q : Float := …; begin Swap (A, B); Swap (P, Q); end Swap; with Gen_Swap; procedure Client is procedure Swap is new Gen_Swap (Some_Type => Integer); procedure Swap is new Gen_Swap (Some_Type => Float); A, B : Integer := …; P, Q : Float := …; begin Swap (A, B); Swap (P, Q); end Swap; Generic instantiation
8
8 8 http://libre.adacore.com © AdaCore under the GNU Free Documentation License Type of Generic Units generic … formal parameters... procedure Proc (…); generic … formal parameters... procedure Proc (…); generic … formal parameters... function Func (…) return …; generic … formal parameters... function Func (…) return …; generic … formal parameters... package Pack is … end Pack; generic … formal parameters... package Pack is … end Pack;
9
9 9 http://libre.adacore.com © AdaCore under the GNU Free Documentation License … formal parameters... Types Objects Subprograms Packages
10
10 http://libre.adacore.com © AdaCore under the GNU Free Documentation License Another example Write a generic function that computes where L & H are integer bounds F is some function returning some type An addition operation is available for this type
11
11 http://libre.adacore.com © AdaCore under the GNU Free Documentation License generic type Res_Type is private; Zero : in Res_Type; with function Add (X, Y : Res_Type) return Res_Type; with function F (I : Integer) return Res_Type; function Sum (L, H : Integer) return Res_Type; generic type Res_Type is private; Zero : in Res_Type; with function Add (X, Y : Res_Type) return Res_Type; with function F (I : Integer) return Res_Type; function Sum (L, H : Integer) return Res_Type; Spec...
12
12 http://libre.adacore.com © AdaCore under the GNU Free Documentation License function Sum (L, H : Integer) return Res_Type is Result : Res_Type := Zero; begin for I in L.. H loop Result := Add (Result, F (I)); end loop; return Result; end Sum; function Sum (L, H : Integer) return Res_Type is Result : Res_Type := Zero; begin for I in L.. H loop Result := Add (Result, F (I)); end loop; return Result; end Sum; Body...
13
13 http://libre.adacore.com © AdaCore under the GNU Free Documentation License with Sum; procedure Client is function Compute (X : Integer) return Integer is … end; function Compute (X : Integer) return Float is … end; function New_Sum is new Sum (Res_Type => Integer, Zero => 0, Add => “+”, F => Compute); function New_Sum is new Sum (Res_Type => Float, Zero => 0.0, Add => “+”, F => Compute); with Sum; procedure Client is function Compute (X : Integer) return Integer is … end; function Compute (X : Integer) return Float is … end; function New_Sum is new Sum (Res_Type => Integer, Zero => 0, Add => “+”, F => Compute); function New_Sum is new Sum (Res_Type => Float, Zero => 0.0, Add => “+”, F => Compute); Instantiating Sum
14
14 http://libre.adacore.com © AdaCore under the GNU Free Documentation License generic type Res_Type is private; Zero : in Res_Type; with function Add (X, Y : Res_Type) return Res_Type; with function F (I : Integer) return Res_Type is <> ; function Sum (L, H : Integer) return Res_Type; generic type Res_Type is private; Zero : in Res_Type; with function Add (X, Y : Res_Type) return Res_Type; with function F (I : Integer) return Res_Type is <> ; function Sum (L, H : Integer) return Res_Type; Default Generic Parameters
15
15 http://libre.adacore.com © AdaCore under the GNU Free Documentation License with Sum; procedure Client is function F (X : Integer) return Integer is … end; function F (X : Integer) return Float is … end; function New_Sum is new Sum (Res_Type => Integer, Zero => 0, Add => “+”); function New_Sum is new Sum (Res_Type => Float, Zero => 0.0, Add => “+”); with Sum; procedure Client is function F (X : Integer) return Integer is … end; function F (X : Integer) return Float is … end; function New_Sum is new Sum (Res_Type => Integer, Zero => 0, Add => “+”); function New_Sum is new Sum (Res_Type => Float, Zero => 0.0, Add => “+”); You can omit F => F You can omit parameter F if there is a visible routine called F with the right parameters
16
16 http://libre.adacore.com © AdaCore under the GNU Free Documentation License Allowed to use “+” “-” “ * ” etc. as function names package Sets is type Set is private; Empty : constant Set; function “+” (S1, S2 : Set) return Set; -- Set union function “*” (S1, S2 : Set) return Set; -- Set intersection function “-” (S1, S2 : Set) return Set; -- Set difference … private type Set is …; end Alerts; package Sets is type Set is private; Empty : constant Set; function “+” (S1, S2 : Set) return Set; -- Set union function “*” (S1, S2 : Set) return Set; -- Set intersection function “-” (S1, S2 : Set) return Set; -- Set difference … private type Set is …; end Alerts;
17
17 http://libre.adacore.com © AdaCore under the GNU Free Documentation License Back to our Generic Sum generic type Res_Type is private; Zero : in Res_Type; with function “+” (X, Y : Res_Type) return Res_Type is <>; with function F (I : Integer) return Res_Type is <> ; function Sum (L, H : Integer) return Res_Type; generic type Res_Type is private; Zero : in Res_Type; with function “+” (X, Y : Res_Type) return Res_Type is <>; with function F (I : Integer) return Res_Type is <> ; function Sum (L, H : Integer) return Res_Type;
18
18 http://libre.adacore.com © AdaCore under the GNU Free Documentation License function Sum (L, H : Integer) return Res_Type is Result : Res_Type := Zero; begin for I in L.. H loop Result := Result + F (I); end loop; return Result; end Sum; function Sum (L, H : Integer) return Res_Type is Result : Res_Type := Zero; begin for I in L.. H loop Result := Result + F (I); end loop; return Result; end Sum; Body...
19
19 http://libre.adacore.com © AdaCore under the GNU Free Documentation License with Sum; with Sets; use Sets; procedure Client is function F (X : Integer) return Integer is … end; function F (X : Integer) return Set is … end; function New_Sum is new Sum (Res_Type => Integer, Zero => 0); function New_Sum is new Sum (Res_Type => Set, Zero => Empty); with Sum; with Sets; use Sets; procedure Client is function F (X : Integer) return Integer is … end; function F (X : Integer) return Set is … end; function New_Sum is new Sum (Res_Type => Integer, Zero => 0); function New_Sum is new Sum (Res_Type => Set, Zero => Empty); You can omit F => F and “+” => “+”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.