Download presentation

Presentation is loading. Please wait.

Published byAugustus Chesbrough Modified over 3 years ago

1
Slide: 1 Copyright © AdaCore Record Types Presented by Quentin Ochem university.adacore.com

2
Slide: 2 Copyright © AdaCore Ada Type Model Type Elementary Type AccessScalar Discrete EnumerationInteger Signed Modular Real Floating PointFixed Point Ordinary Decimal Composite Type Array Record Protected Task

3
Slide: 3 Copyright © AdaCore Record Types Allow to store named heterogeneous data in a type Fields are accessed through dot notation type Shape is record Id : Integer; X, Y : Float; end record; S : Shape; begin S.X := 0.0; S.Id := 1;

4
Slide: 4 Copyright © AdaCore Nested Record Types Any kind of definite type can be used as component types Size may only be known at elaboration time –Has impact on code generated type Position is record X, Y : Integer; end record; type Shape is record Name : String (1.. 10); P : Position; end record; Len : Natural := Compute_Len; type Name_Type is new String (1.. Len); type Shape is record Name : Name_Type; P : Position; end record;

5
Slide: 5 Copyright © AdaCore Default Values Default Values can be provided to record components: Default values are dynamic expressions evaluated at object elaboration type Position is record X : Integer := 0; Y : Integer := 0; end record; Cx, Cy : Integer := 0; type Position is record X : Integer := Cx; Y : Integer := Cy; end record; P1 : Position; -- = (0, 0); begin Cx := 1; Cy := 1; declare P2 : Position; -- = (1, 1);

6
Slide: 6 Copyright © AdaCore Aggregates (1/2) Like arrays, record values can be given through aggregates Named aggregates are possible (but cannot switch back to positional) type Position is record X, Y : Integer; end record; type Shape is record Name : String (1.. 10); P : Position; end record; Center : Position := (0, 0); Circle : Shape := ((others => ' '), Center); P1 : Position := (0, Y => 0); -- OK P2 : Position := (X => 0, Y => 0); -- OK P3 : Position := (Y => 0, X => 0); -- OK P4 : Position := (X => 0, 0); -- NOK

7
Slide: 7 Copyright © AdaCore Aggregates (2/2) Named aggregate is required for one-elements records Default values can be referred as <> after a name or others If all remaining types are the same, others can use an expression type Singleton is record V : Integer; end record; V1 : Singleton := (V => 0); -- OK V2 : Singleton := (0); -- NOK type Rec is record A, B, C, D : Integer; end record; V1 : Rec := (others => <>); V2 : Rec := (A => 0, B => <>, others => <>); type Rec is record A, B : Integer; C, D : Float; end record; V1 : Rec := (0, 0, others => 0.0);

8
Slide: 8 Copyright © AdaCore Discriminant Rationale Only a subset of the components are needed to use this type, depending on the context Why do we need to use the memory for Radius if the shape is a line? type Shape is record X, Y : Float; X2, Y2 : Float; Radius : Float; Outer_Radius : Float; end record;

9
Slide: 9 Copyright © AdaCore Use of a Discriminant Types can be parameterized after a discrete type This type is indefinite, need to be constrained at object declaration type Shape_Kind is (Circle, Line, Torus); type Shape (Kind : Shape_Kind) is record X, Y : Float; case Kind is when Line => X2, Y2 : Float; when Torus => Outer_Radius, Inner_Radius : Float; when Circle => Radius : Float; end case; end record; V : Shape (Circle);

10
Slide: 10 Copyright © AdaCore Usage of a Record with Discriminant As for arrays – the unconstrained part has to be specified Accessing to a component not accessible for a given constraint will raise Constraint_Error Note: A discriminant is constant, set at object declaration V1 : Shape (Circle) := …; V2 : Shape := V1; -- OK, constrained by initialization begin V1.Radius := 0.0; -- OK, radius is in the Circle case V2.X2 := 0.0; -- Raises constraint error

11
Slide: 11 Copyright © AdaCore Aggregates with Discriminants Same as record aggregates – but have to give a value to the discriminant Only the values related to the constraint are supplied V1 : Shape := (Kind => Line, X => 0.0, Y => 0.0, X2 => 10.0, Y2 => 10.0); V2 : Shape := (Circle, 0.0, 0.0, 5.0);

12
Slide: 12 Copyright © AdaCore Quiz

13
Slide: 13 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? type R is record A, B, C : Integer := 0; end record; V : R := (A => 1); (1/10)

14
Slide: 14 Copyright © AdaCore NO Is this correct? (1/10) type R is record A, B, C : Integer := 0; end record; V : R := (A => 1); Compilation error, the aggregate should give a default value for other fields, for example (A => 1, others => <>)

15
Slide: 15 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (2/10) type My_Integer is new Integer; type R is record A, B, C : Integer := 0; D : My_Integer := 0; end record; V : R := (others => 1);

16
Slide: 16 Copyright © AdaCore NO Is this correct? (2/10) type My_Integer is new Integer; type R is record A, B, C : Integer := 0; D : My_Integer := 0; end record; V : R := (others => 1); Compilation error, all components are not of the same type, they can't be given a common value through others

17
Slide: 17 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (3/10) type Cell is record Val : Integer; Next : Cell; end record;

18
Slide: 18 Copyright © AdaCore NO Is this correct? (3/10) type Cell is record Val : Integer; Next : Cell; end record; Compilation error, this type definition is recursive

19
Slide: 19 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (4/10) type My_Integer is new Integer; type R is record A, B, C : Integer; D : My_Integer; end record; V : R := (others => <>);

20
Slide: 20 Copyright © AdaCore YES Is this correct? (4/10) type My_Integer is new Integer; type R is record A, B, C : Integer; D : My_Integer; end record; V : R := (others => <>); This is correct. In the absence of explicit values given in the record definition, A, B, C and D will be of whatever value is in the memory at this time

21
Slide: 21 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (5/10) type R is record A : Integer := 0; end record; V : R := (0);

22
Slide: 22 Copyright © AdaCore NO Is this correct? (5/10) type R is record A : Integer := 0; end record; V : R := (0); Compilation error, only the named notation is allowed in singleton values, e.g. (A => 0)

23
Slide: 23 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (6/10) type R is record V : String; end record; V : R := (V => "Hello");

24
Slide: 24 Copyright © AdaCore NO Is this correct? (6/10) type R is record V : String; end record; V : R := (V => "Hello"); Compilation error, a record can't have an unconstrained component

25
Slide: 25 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (7/10) type R is record S : String (1.. 10); end record; V : R := (S => "Hello");

26
Slide: 26 Copyright © AdaCore NO Is this correct? (7/10) type R is record S : String (1.. 10); end record; V : R := (S => "Hello"); Run-time error (and possible a compile-time warning). The size of the string given here is 5, while we expect a string of 10 elements

27
Slide: 27 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (8/10) type R (D : Integer) is record null; end record; V1 : R := (D => 5); V2 : R := (D => 6); begin V1 := V2;

28
Slide: 28 Copyright © AdaCore NO Is this correct? (8/10) type R (D : Integer) is record null; end record; V1 : R := (D => 5); V2 : R := (D => 6); begin V1 := V2; V1 and V2 have different discriminant values, they're considered structurally different

29
Slide: 29 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (9/10) type Shape_Kind is (Circle, Line); type Shape (Kind : Shape_Kind) is record case Kind is when Line => X, Y : Float; X2, Y2 : Float; when Circle => X, Y : Float; Radius : Float; end case; end record;

30
Slide: 30 Copyright © AdaCore NO Is this correct? (9/10) type Shape_Kind is (Circle, Line); type Shape (Kind : Shape_Kind) is record case Kind is when Line => X, Y : Float; X2, Y2 : Float; when Circle => X, Y : Float; Radius : Float; end case; end record; X and Y components are duplicated

31
Slide: 31 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (10/10) type Shape_Kind is (Circle, Line); type Shape (Kind : Shape_Kind) is record X, Y : Float; case Kind is when Line => X2, Y2 : Float; when Circle => Radius : Float; end case; end record; V : Shape := (Circle, others => <>); begin V.Kind := Line; V.X2 := 0.0; V.Y2 := 0.0;

32
Slide: 32 Copyright © AdaCore NO Is this correct? (10/10) type Shape_Kind is (Circle, Line); type Shape (Kind : Shape_Kind) is record X, Y : Float; case Kind is when Line => X2, Y2 : Float; when Circle => Radius : Float; end case; end record; V : Shape := (Circle, others => <>); begin V.Kind := Line; V.X2 := 0.0; V.Y2 := 0.0; The discriminant of an object is constant

33
Slide: 33 Copyright © AdaCore university.adacore.com

Similar presentations

OK

C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.

C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To ensure the functioning of the site, we use **cookies**. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy & Terms.
Your consent to our cookies if you continue to use this website.

Ads by Google

Ppt on role of it in indian railways Ppt on nepal culture Download ppt on electron beam machining Ppt on energy giving food meaning Ppt on sound navigation and ranging systematic Ppt on cross multiplication method of linear equations Ppt on thermal conductivity of insulating powder Ppt on regional trade agreements pdf Ppt on school library management system Ppt on solar system for grade 5