Difference between revisions of "Type Variables"

From Mesham
Jump to navigationJump to search
m
m (4 revisions imported)
 
(One intermediate revision by the same user not shown)
Line 13: Line 13:
 
== Example ==
 
== Example ==
  
  typevar m::=Int :: allocated[multiple[]];
+
  function void main() {
var f:m;
+
    typevar m::=Int :: allocated[multiple[]];
typevar q::=declaredtype f;
+
    var f:m;
q::=m;
+
    typevar q::=declaredtype f;
 +
    q::=m;
 +
};
  
 
In the above code example, the type variable ''m'' has the type value ''Int :: allocated[multiple[]]'' assigned to it. On line 2, a new (program) variable is created using this new type variable. In line 3, the type variable ''q'' is declared and has the value of the declared type of program variable ''f''. Lastly in line 4, type variable ''q'' changes its value to become that of type variable ''m''. Although type variables can be thought of as the programmer creating new types, they can also be used like program variables in cases such as equality tests and assignment.
 
In the above code example, the type variable ''m'' has the type value ''Int :: allocated[multiple[]]'' assigned to it. On line 2, a new (program) variable is created using this new type variable. In line 3, the type variable ''q'' is declared and has the value of the declared type of program variable ''f''. Lastly in line 4, type variable ''q'' changes its value to become that of type variable ''m''. Although type variables can be thought of as the programmer creating new types, they can also be used like program variables in cases such as equality tests and assignment.

Latest revision as of 15:44, 15 April 2019

Syntax

typevar name::=type; name::=type;

Note how ::= is used rather than := typevar is the type equivalent of var

Semantics

Type variables allow the programmer to assign types and type combinations to variables for use as normal program variables. These exist only statically (in compilation) and are not present in the runtime semantics.

Example

function void main() {
   typevar m::=Int :: allocated[multiple[]];
   var f:m;
   typevar q::=declaredtype f;
   q::=m;
};

In the above code example, the type variable m has the type value Int :: allocated[multiple[]] assigned to it. On line 2, a new (program) variable is created using this new type variable. In line 3, the type variable q is declared and has the value of the declared type of program variable f. Lastly in line 4, type variable q changes its value to become that of type variable m. Although type variables can be thought of as the programmer creating new types, they can also be used like program variables in cases such as equality tests and assignment.

Since: Version 0.5