Difference between revisions of "Record"

From Mesham
Jump to navigationJump to search
(Created page with '== Syntax == record[name<sub>1</sub>,type<sub>1</sub>,name<sub>2</sub>,type<sub>2</sub>,.....,name<sub>d</sub>,type<sub>d</sub>] == Semantics == The ''record'' type allows the…')
 
(Example)
Line 9: Line 9:
 
== Example ==
 
== Example ==
  
  var complex : record["r",Float,"i",Float];
+
  typevar complex ::= record["r",Float,"i",Float];
  var person: record["name",String, "age",Int, "gender",Char];
+
  var a:array[complex, 10];
  var a:array[complex,10];
+
var number:complex;
  (a#1).i:=22.3;
+
var pixel : record["r",Int,"g",Int,"b",Int];
  var b:complex;
+
   
var me:person;
+
  a[1].r:=8.6;
  me.name:="nick";
+
  number.i:=3.22;
 +
  pixel.b:=128;
  
In the above example, ''complex'' (a complex number) is a record with two [[float]] elements, ''i'' and ''r''. The variable ''b'' is defined as a complex number and ''a'' as an array of these numbers. The variable ''me'' is of type ''person''.  
+
In the above example, ''complex'' is declared as a [[Type_Variables|type variable]] to be a complex number. This is then used as the type chain for ''a'' which is an [[array]] and ''number''. Using records in this manner can be useful, although the other way is just to include directly in the type chain for a variable such as declaring the ''pixel'' variable. Do not get confused between the difference between ''complex'' (a type variable existing during compilation only) and ''pixel'' (a normal data variable which exists at runtime.) In the last two lines assignment occurs to the declared variables.
  
 
[[Category:Type Library]]
 
[[Category:Type Library]]
 
[[Category:Composite Types]]
 
[[Category:Composite Types]]
 
[[Category:Composition Types]]
 
[[Category:Composition Types]]

Revision as of 17:42, 12 January 2013

Syntax

record[name1,type1,name2,type2,.....,named,typed]

Semantics

The record type allows the programmer to combine d attributes into one, new type. There can be any number of names and types inside the record type. A record type is very similar to a typedef structure in C. To access the member of a record use the dot, .

Example

typevar complex ::= record["r",Float,"i",Float];
var a:array[complex, 10];
var number:complex;
var pixel : record["r",Int,"g",Int,"b",Int];

a[1].r:=8.6;
number.i:=3.22;
pixel.b:=128;

In the above example, complex is declared as a type variable to be a complex number. This is then used as the type chain for a which is an array and number. Using records in this manner can be useful, although the other way is just to include directly in the type chain for a variable such as declaring the pixel variable. Do not get confused between the difference between complex (a type variable existing during compilation only) and pixel (a normal data variable which exists at runtime.) In the last two lines assignment occurs to the declared variables.