Difference between revisions of "Record"

From Mesham
Jump to navigationJump to search
m
Line 16: Line 16:
 
== Example ==
 
== Example ==
  
  typevar complex ::= record["r",Float,"i",Float];
+
  function void main() {
var a:array[complex, 10];
+
    typevar complex ::= record["r",Float,"i",Float];
var number:complex;
+
    var a:array[complex, 10];
var pixel : record["r",Int,"g",Int,"b",Int];
+
    var number:complex;
 +
    var pixel : record["r",Int,"g",Int,"b",Int];
 
   
 
   
a[1].r:=8.6;
+
    a[1].r:=8.6;
number.i:=3.22;
+
    number.i:=3.22;
pixel.b:=128;
+
    pixel.b:=128;
 +
};
  
 
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.
 
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.

Revision as of 14:24, 15 April 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, .

Default typing

Example

function void main() {
   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.

Since: Version 0.41b