Difference between revisions of "Tutorial - Advanced Types"

From Mesham
Jump to navigationJump to search
m (4 revisions imported)
 
(No difference)

Latest revision as of 15:45, 15 April 2019

Tutorial number ten - prev

Introduction

Mesham has a number of advanced typing features over and above type chains and type coercion. In this tutorial we will look at some of this, how they might be used and how they can simplify your program code.

Type Variables

The language has a concept of a type variable, which is a, compilation time, programmer defined type representing a more complex type chain. Let's have a look at this in more detail via an example

function void main() {
   typevar typeA::=Int::allocated[multiple];
   typevar typeB::=String::allocated[single[on[3]]];

   var a:typeA;
   var b:typeB;
};

In this example we create type type variables called typeA and typeB which represent different type chains. Then the actual program variables a and b are declared using these type variables. Notice how type assignment is using the ::= operator rather than normal program variable assignment which folows :=.

function void main() {
   typevar typeA::=Int::allocated[multiple];
   var a:typeA;

   typeA::=String;
   var b:typeA;

   typeA::=typeA::const;
   var c:typeA;
};

This example demonstrates assigning types and chains to existing type variables. At lines two and three we declare the type variable typeA and use it in the declaration of program variable a. However, then on line five we modify the value of the type variable, typeA using the ::= operator to be a String instead. Then on line six we declare variable b using this type variable, which effectively sets the type to be a String. Line eight demonstrates how we can use the type variable in type chain modification and variable c is a constant String.

Note: It is important to appreciate that type variables exist only during compilation, they do not exist at runtime and as such can not be used in conditional statements.

Types of program variables

Mesham provides some additional keywords to help manage and reference the type of program variables, however it is imperative to remember that these are static only i.e. only exist during compilation.

Currenttype

Mesham has an inbuilt currenttype keyword which will result in the current type chain of a program variable.

a:currenttype a :: const;
a:a::const

In this code snippet both lines of code are identical, they will set the type of program variable a to be the current type chain combined with the const type. Note that using a program variable in a type chain such as in the snippet above is a syntactic short cut for the current type (using the currenttype keyword) and either can be used.

Declaredtype

It can sometimes be useful to reference or even revert back to the declared type of a program variable later on in execution. To do this we supply the declaredtype keyword.

function void main() {
   var a:Int;
   a:a::const;
   a:declaredtype a;
   a:=23;
};

This code will compile and work fine because, although we are coercing the type of a to be that of the const type at line three, on line four we are reverting the type to be the declared type of the program variable. If you are unsure about why this is the case, then move the assignment around to see when the code will not compile with it.

An example

Type variables are commonly used with records and referencerecords. In fact, the complex type obtained from the maths library is in fact a type variable.

#include <string>
#include <io>

typevar node;
node::=referencerecord[Int, "data", node, "next"];
function void main() {
   var i;
   var root:node;
   root:=null;
   for i from 0 to 9 {
      var newnode:node;
      newnode.data:=i;
      newnode.next:=root;
      root:=newnode;
   };
   while (root != null) {
      print(itostring(root.data)+"\n");
      root:=root.next;
   };
};

This code will build up a linked list of numbers and then walk it, displaying each number as it goes. Whilst it is a relatively simple code, it illustrates how one might use type variables to improve the readability of their code. One important point to note is a current limitation in the Mesham parser and that is the fact that we are forced to declare the type variable node on line four and then separately assign to it at line five. The reason for this is that in this assignment we are referencing back to the node type variable in the referencerecord type and as such it must exist.

Limitations

There are some important limitations to note about the current use of types. Types currently only exist explicitly during compilation - what this means is that you can not do things such as passing them into functions or communicating them. Additionally, once allocation information (the allocated type) and its subtypes have been set then you can not modify this, nor can you change the element type.