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

Slides:



Advertisements
Similar presentations
Symbol Table.
Advertisements

Variables and Operators
Slide: 1 Copyright © AdaCore Packages Presented by Quentin Ochem university.adacore.com.
Programming Languages and Paradigms The C Programming Language.
Slide: 1 Copyright © AdaCore Arrays Presented Quentin Ochem university.adacore.com.
Fortran Jordan Martin Steven Devine. Background Developed by IBM in the 1950s Designed for use in scientific and engineering fields Originally written.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Chapter 5 Names, Bindings, and Scopes
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Generic programming in Java
Array Types Index types can be of any discrete type Component type must be definite, i.e. have bounds: type class_list is array ( ) of String (1..10);
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
ITEC 320 Lecture 23 OO in Ada. OO in Ada(1) Review Questions? Exam 2 next Friday ADTs.
Approaches to Typing Programming Languages Robert Dewar.
Type Checking  Legality checks  Operator determination  Overload resolution.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
Chapter 1 Program Design
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Slide: 1 Copyright © AdaCore Basic Types Presented by Quentin Ochem university.adacore.com.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
General Computer Science for Engineers CISC 106 Lecture 04 Dr. John Cavazos Computer and Information Sciences 09/10/2010.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
ITEC 320 Lecture 13 OO in Ada. OO in Ada(1) Review Questions? HW 3 due tomorrow night.
Chapter 5 Names, Bindings, and Scopes. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 5 Topics Introduction Names Variables The Concept.
Lecture #5 Introduction to C++
Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
Parameter Passing Mechanisms Reference Parameters Read § §
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Parameter Passing Mechanisms Reference Parameters § §
August 6, 2009 Data Types, Variables, and Arrays.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
1 CS Programming Languages Class 08 September 19, 2000.
Bindings. Typical stages of program execution First, the program is compiled Then everything is loaded into memory Then it is linked to any library routines.
Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com.
1 Bindings. 2 Outline Preliminaries Scope  Block structure  Visibility Static vs. dynamic binding Declarations and definitions More about blocks The.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1D Arrays and Random Numbers Artem A. Lenskiy, PhD May 26, 2014.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Data Types Chapter 6: Data Types Lectures # 12. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
Records type city is record -- Ada Name: String (1..10); Country : String (1..20); Population: integer; Capital : Boolean; end record; struct city { --
Data Types In Text: Chapter 6.
Programming Languages and Compilers (CS 421)
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
Types and Values.
Records Design Issues: 1. What is the form of references?
Introduction to the C Language
Variables and Constants
Python fundamental.
Presentation transcript:

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

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

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;

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;

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);

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

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);

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;

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);

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

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);

Slide: 12 Copyright © AdaCore Quiz

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)

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 => <>)

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);

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

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;

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

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 => <>);

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

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);

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)

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");

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

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");

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

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;

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

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;

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

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;

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

Slide: 33 Copyright © AdaCore university.adacore.com