records
play

Records A Tuple (Mathematically): is a function f : { 0, 1, , n 1 - PowerPoint PPT Presentation

Records A Tuple (Mathematically): is a function f : { 0, 1, , n 1 } Set Example: ( 5, 3.1, 6.0, c , foo , 13, 14, d ) J.Carette (McMaster) CS 2S03 1 / 19 Records A Tuple (Mathematically): is a


  1. Records A Tuple (Mathematically): is a function f : { 0, 1, · · · , n − 1 } → Set Example: ( 5, − 3.1, 6.0, ′ c ′ , ′′ foo ′′ , 13, 14, ′ d ′ ) J.Carette (McMaster) CS 2S03 1 / 19

  2. Records A Tuple (Mathematically): is a function f : { 0, 1, · · · , n − 1 } → Set Example: ( 5, − 3.1, 6.0, ′ c ′ , ′′ foo ′′ , 13, 14, ′ d ′ ) bad style !: Instead of implicit labels use explicit labels! Tuples (Programming Languages): usually of named fields tuple of named fields ≡ records Example: labels: { latitude, longitude, altitude } record: { latitude = 48.715, longitude = 2.208, altitude = 156 } J.Carette (McMaster) CS 2S03 1 / 19

  3. Defining a Record (Java) class Point { final double latitude; final double longitude; final double altitude ;} J.Carette (McMaster) CS 2S03 2 / 19

  4. Records - Declaration Point x; x r null J.Carette (McMaster) CS 2S03 3 / 19

  5. Records - Declaration Point x; x r null new Point (); x 0.0 0.0 0.0 The value of this expression is the reference r ′ = { latitude = 0.0, longitude = 0.0, altitude = 0.0 } J.Carette (McMaster) CS 2S03 3 / 19

  6. Records - Allocation x = new Point (); x 0.0 0.0 0.0 the environment is e ⊕ [ x = r ] the memory is [ r = r ′ , r ′ = { latitude = 0.0, longitude = 0.0, altitude = 0.0 } ] You can also write: Point x = new Point (); J.Carette (McMaster) CS 2S03 4 / 19

  7. Records - Allocation - Cont. The following statement creates the environment [ x = r ′ ] and the memory state [ r ′ = { latitude = 0.0, longitude = 0.0, altitude = 0.0 } ] . final Point x = new Point (); x 0.0 0.0 0.0 J.Carette (McMaster) CS 2S03 5 / 19

  8. Records - Accessing Fields x.latitude x.thing : error at compile time Consider an expression x with x mutable of type Point . Then value of x is m ( e ( x ) (a record) and the value of x . latitude is that of the field latitude of m ( m ( e ( x )) . x e" m" m" 0.0 0.0 0.0 J.Carette (McMaster) CS 2S03 6 / 19

  9. Assignment of Fields class Point { final double latitude; final double longitude; final double altitude ;} Point x = new Point (); x x 0.0 0.0 0.0 ✔ ! ✖ ! 0.0 0.0 0.0 The compiler “knows” which fields are mutable and not and will enforce it! J.Carette (McMaster) CS 2S03 7 / 19

  10. Constructs for Handling Records define a type: class allocate a cell: new access a field: t.l assign to a field: t.l = u; J.Carette (McMaster) CS 2S03 8 / 19

  11. Constructors x = new Point (48.715 , 2.208 , 156.0); To use, we need to change the class and add a constructor: Point (final double x, final double y, final double z) { this.latitude = x; this.longitude = y; this.altitude = z;} a constructor must always have the same name as the type it belongs to, in the definition of a constructor, you never define a return type, you never use the return statement in the body of a constructor, in the body of a constructor, you can assign to fields of the allocated record using the keyword this to refer to this record. J.Carette (McMaster) CS 2S03 9 / 19

  12. Constructors - Cont. 1 Constructors can be overloaded ! J.Carette (McMaster) CS 2S03 10 / 19

  13. Constructors - Cont. 1 Constructors can be overloaded ! 2 When no constructor is defined, there is a default constructor with no arguments. You cannot use the default constructor once one has been defined in your program. ( bad style! ) J.Carette (McMaster) CS 2S03 10 / 19

  14. Constructors - Cont. 1 Constructors can be overloaded ! 2 When no constructor is defined, there is a default constructor with no arguments. You cannot use the default constructor once one has been defined in your program. ( bad style! ) 3 When declaring a type, it is also possible to define default values for each field: class Point { double latitude = 90.0; double longitude = 0.0; double altitude = 0.0;} J.Carette (McMaster) CS 2S03 10 / 19

  15. The Semantics of Records To cover records: we need to add a fifth argument to the functions Σ and Θ : the list of constructed types, each type being associated to an ordered pair composed: T �→ ( list of fields, list of constructors ) J.Carette (McMaster) CS 2S03 11 / 19

  16. Sharing In the first case (sharing), all changes of the cell associated with x automatically change the cell associated with y, and vice versa. x ! x x ! x 48.715 ! 2.208 ! 156.0 ! 48.715 2.208 156.0 48.715 ! 2.208 ! 156.0 ! 48.715 2.208 156.0 y ! y ! y y 90.0 0.0 0.0 90.0 ! 90.0 0.0 0.0 0.0 ! 0.0 ! 90.0 ! 0.0 ! 0.0 ! x = y (Sharing!) y,lat = x.lat x ! x y.long = x.long 48.715 ! 2.208 ! 156.0 ! 48.715 2.208 156.0 y.alt = x.alt y ! y 48.715 ! 2.208 ! 156.0 ! 48.715 2.208 156.0 J.Carette (McMaster) CS 2S03 12 / 19

  17. Equality a = new Point (1, 2, 3); b = new Point (1, 2, 3); Two types of equality: Physical. Two records of the same type are physically equivalent (a==b) only when they are identical (share the same cell). Hence, a == b is false. Structural. Two records of the same type are structural equivalent when their field’s value are equal. Hence, a and b are structural equivalent. J.Carette (McMaster) CS 2S03 13 / 19

  18. Wrapper Types (from Base type to Object type) A wrapper is a type of record with one lone field. class Integer { int c; Integer (int x) { this.c = x; } } Why? 1 Uniformity 2 Sharing J.Carette (McMaster) CS 2S03 14 / 19

  19. Wrapper Types - Cont. y ! y x ! x 4 ! 5 ! 4 5 Integer x = new Integer(4); int x = 4; Integer y = new Integer(x.c); int y = x; x.c = 5; x = 5; System.out.println(y.c); System.out.println(y); prints 4 prints 4 If we replace the second line with Integer y = x then it prints 5 x ! x 5 ! 5 y ! y J.Carette (McMaster) CS 2S03 15 / 19

  20. Wrapper Types - Cont. The following function swaps values of x and y. static void swap (Integer x, Integer y) { int temp = x.c; x.c = y.c; y.c = temp ;} Remember, the following functions do nothing on x and y: static void swp (int x, int y) { int temp = x; x = y; y = temp ;} J.Carette (McMaster) CS 2S03 16 / 19

  21. Wrapper Types - Cont. When we call the function swap(a, b) , we create the state: b ! b a ! a 7 ! 7 4 ! 4 x ! y ! y x and so swapping the contents of x and y will also swap those of a and b. If a, b are constant ( final ), and x, y too, then: a ! x ! b ! y ! a x b y 4 ! 7 ! 4 7 and swap works too. J.Carette (McMaster) CS 2S03 17 / 19

  22. Records in Caml 1 Record Type: type point = { latitude : double; longitude : double; altitude : double ;} 2 Creating: let x = {lat = 90.0; longi = 0.0; altitude = 0.0;} or let y = ref{lat = 90.0; longi = 0.0; altitude = 0.0;} 3 Accessing: x.latitude (!y). latitude 4 Assigning: all fields are constant by default! type int -wrap = {mutable c: int} let x = {c = ref 5} x.c <- 4 J.Carette (McMaster) CS 2S03 18 / 19

  23. Records in C 1 Record Type: struct Point { double latitude; double longitude; double altitude ;}; 2 Creating: struct Point x; or struct Point x = {5.0 ,7.0 ,100.0}; 3 Accessing: x.latitude 4 Assigning: all fields are constant by default! x.latitude = 12.0 Call by copy! (the value of x is not a reference associated with a record in memory as it is in Java and Caml!) J.Carette (McMaster) CS 2S03 19 / 19

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend