pub struct TyVar(/* private fields */);Expand description
An inference variable — a placeholder for a type not yet known.
A TyVar is a 32-bit handle minted by Unifier::fresh
and stable for the life of that unifier. It is deliberately opaque: there is no
public constructor, so a variable can only be obtained from a unifier. Mint and
use variables in the same unifier — a handle is an index into that unifier’s id
space, and a unifier treats an id it has not minted as an unbound variable, so a
handle is only meaningful against the unifier that produced it.
A variable starts life unbound — it stands for “some type” — and unification
may bind it to a concrete type or to another variable. Use
Unifier::resolve to read back what a variable has
become; a variable that is still unbound resolves to itself.
§Examples
use type_lang::Unifier;
let mut unifier = Unifier::new();
let a = unifier.fresh();
let b = unifier.fresh();
// Variables are minted in order and stay distinct.
assert_eq!(a.to_u32(), 0);
assert_eq!(b.to_u32(), 1);
assert_ne!(a, b);Implementations§
Source§impl TyVar
impl TyVar
Sourcepub const fn to_u32(self) -> u32
pub const fn to_u32(self) -> u32
Returns the raw index this variable wraps.
The value is the variable’s creation order, starting at 0. It is useful as
a dense key into a side table of per-variable data; prefer passing the
opaque TyVar itself wherever a handle will do.
§Examples
use type_lang::Unifier;
let mut unifier = Unifier::new();
let v = unifier.fresh();
assert_eq!(v.to_u32(), 0);