Linear type system

From Wikipedia, the free encyclopedia

A linear type system is a particular form of type system used in a programming language. Linear type systems allow references but not aliases. To enforce this, a reference goes out of scope after appearing on the right-hand side of an assignment. Note that passing a reference as an argument to a function is a form of assignment, as the function parameter will be assigned the value inside the function, and therefore such use of a reference also causes it to go out of scope. Linear typing is related to uniqueness typing but is generally more restrictive.

A linear type system is similar to C++'s auto_ptr class, which behaves like a pointer but is invalidated by being set to null after use in an assignment. However, the linearity constraint can be checked at compile time, whereas auto_ptr can only raise exceptions at run time if it is misused.

[edit] Example code

Dog* d = new Dog( name="Fido" ); //creating a reference to a new object
Dog* p = d;                      //creating another reference to the same object
print p->getName();              //output "Fido"
print d->getName();              //ILLEGAL. d has been invalidated, because it is an alias for p