Difference between revisions of "Referencerecord"
m |
|||
Line 21: | Line 21: | ||
typevar node; | typevar node; | ||
node::=referencerecord["prev",node,"Int",data,"next",node]; | node::=referencerecord["prev",node,"Int",data,"next",node]; | ||
− | var head:node; | + | function void main() { |
− | + | var head:node; | |
− | + | head:=null; | |
− | + | var i; | |
− | + | for i from 0 to 9 { | |
− | + | var newnode:node; | |
− | + | newnode.data:=i; | |
− | + | newnode.next:=head; | |
− | + | if (head!=null) head.prev:=newnode; | |
− | + | head:=newnode; | |
+ | }; | ||
− | + | while (head != null) { | |
− | + | print(itostring(head.data)+"\n"); | |
− | + | head:=head.next; | |
+ | }; | ||
}; | }; | ||
Revision as of 14:25, 15 April 2013
Contents
Syntax
referencerecord[name1, type1,name2,type2,...,named,typed]
Semantics
The record type may NOT refer to itself (or other records) where as reference records support this, allowing the programmer to create data structures such as linked lists and trees. There are some added complexities of reference records, such as communicating them (all links and linking nodes will be communicated with the record) and freeing the data (garbage collection.) This results in a slight performance hit and is the reason why the record concept has been split into two types.
Default typing
Currently communication is not available for reference records, this will be fixed at some point in the future.
Example
#include <io> #include <string> typevar node; node::=referencerecord["prev",node,"Int",data,"next",node]; function void main() { var head:node; head:=null; var i; for i from 0 to 9 { var newnode:node; newnode.data:=i; newnode.next:=head; if (head!=null) head.prev:=newnode; head:=newnode; }; while (head != null) { print(itostring(head.data)+"\n"); head:=head.next; }; };
In this code example a doubly linked list is created, and then its contents read node by node.
Since: Version 0.5