- Linear type system
A linear type system is a particular form of
type system used in aprogramming 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, thus ensuring that only one reference to any object exists at once. 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 touniqueness typing but is generally more restrictive.A linear type system is similar to
C++ 'sauto_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 atcompile time , whereas auto_ptr can only raise exceptions atrun time if it is misused.Example code
This example uses C++-like notation, but shows a language with a linear type system (which therefore is "not" C++).
Dog* d = new Dog( name="Fido" ); // creates a reference to a new objectDog* p = d; // uses d to create a reference to the same object: this forces d out of scopeprint p->getName(); // output "Fido"print d->getName(); // COMPILE-TIME ERROR: d was forced out of scope by its use above and is not a valid variable here
Wikimedia Foundation. 2010.