Ada 95 - Generics
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,356
On Slideshare
1,351
From Embeds
5
Number of Embeds
2

Actions

Shares
Downloads
15
Comments
0
Likes
0

Embeds 5

http://www.slideshare.net 3
http://www.slideee.com 2

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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