Struct arithmetic_typing::TypeEnvironment
source · pub struct TypeEnvironment<Prim: PrimitiveType = Num> { /* private fields */ }
Expand description
Environment containing type information on named variables.
§Examples
See the crate docs for examples of usage.
§Concrete and partially specified types
The environment retains full info on the types even if the type is not
concrete. Non-concrete types are tied to an environment.
An environment will panic on inserting a non-concrete type via Self::insert()
or other methods.
// An easy way to get a non-concrete type is to involve `any`.
let code = "(x, ...) = (1, 2, 3) as any;";
let code = Parser::parse_statements(code)?;
let mut env: TypeEnvironment = Prelude::iter().collect();
env.process_statements(&code)?;
assert!(!env["x"].is_concrete());
Implementations§
source§impl<Prim: PrimitiveType> TypeEnvironment<Prim>
impl<Prim: PrimitiveType> TypeEnvironment<Prim>
sourcepub fn iter(&self) -> impl Iterator<Item = (&str, &Type<Prim>)> + '_
pub fn iter(&self) -> impl Iterator<Item = (&str, &Type<Prim>)> + '_
Iterates over variables contained in this env.
sourcepub fn insert_constraint(
&mut self,
constraint: impl Constraint<Prim>,
) -> &mut Self
pub fn insert_constraint( &mut self, constraint: impl Constraint<Prim>, ) -> &mut Self
Inserts a Constraint
into the environment so that it can be used when parsing
type annotations.
Adding a constraint is not mandatory for it to be usable during type inference; this method only influences whether the constraint is recognized during type parsing.
sourcepub fn insert_object_safe_constraint(
&mut self,
constraint: impl ObjectSafeConstraint<Prim>,
) -> &mut Self
pub fn insert_object_safe_constraint( &mut self, constraint: impl ObjectSafeConstraint<Prim>, ) -> &mut Self
Inserts an ObjectSafeConstraint
into the environment so that it can be used
when parsing type annotations.
Other than more strict type requirements, this method is identical to
Self::insert_constraint
.
sourcepub fn process_statements<'a, T>(
&mut self,
block: &Block<'a, T>,
) -> Result<Type<Prim>, Errors<Prim>>where
T: Grammar<Type<'a> = TypeAst<'a>>,
NumArithmetic: MapPrimitiveType<T::Lit, Prim = Prim> + TypeArithmetic<Prim>,
pub fn process_statements<'a, T>(
&mut self,
block: &Block<'a, T>,
) -> Result<Type<Prim>, Errors<Prim>>where
T: Grammar<Type<'a> = TypeAst<'a>>,
NumArithmetic: MapPrimitiveType<T::Lit, Prim = Prim> + TypeArithmetic<Prim>,
Processes statements with the default type arithmetic. After processing, the environment will contain type info about newly declared vars.
This method is a shortcut for calling process_with_arithmetic
with
NumArithmetic::without_comparisons()
.
sourcepub fn process_with_arithmetic<'a, T, A>(
&mut self,
arithmetic: &A,
block: &Block<'a, T>,
) -> Result<Type<Prim>, Errors<Prim>>where
T: Grammar<Type<'a> = TypeAst<'a>>,
A: MapPrimitiveType<T::Lit, Prim = Prim> + TypeArithmetic<Prim>,
pub fn process_with_arithmetic<'a, T, A>(
&mut self,
arithmetic: &A,
block: &Block<'a, T>,
) -> Result<Type<Prim>, Errors<Prim>>where
T: Grammar<Type<'a> = TypeAst<'a>>,
A: MapPrimitiveType<T::Lit, Prim = Prim> + TypeArithmetic<Prim>,
Processes statements with a given arithmetic
. After processing, the environment
will contain type info about newly declared vars.
§Errors
Even if there are any type errors, all statements in the block
will be executed
to completion and all errors will be reported. However, the environment will not
include any vars beyond the first failing statement.
Trait Implementations§
source§impl<Prim: Clone + PrimitiveType> Clone for TypeEnvironment<Prim>
impl<Prim: Clone + PrimitiveType> Clone for TypeEnvironment<Prim>
source§fn clone(&self) -> TypeEnvironment<Prim>
fn clone(&self) -> TypeEnvironment<Prim>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<Prim: Debug + PrimitiveType> Debug for TypeEnvironment<Prim>
impl<Prim: Debug + PrimitiveType> Debug for TypeEnvironment<Prim>
source§impl<Prim: PrimitiveType> Default for TypeEnvironment<Prim>
impl<Prim: PrimitiveType> Default for TypeEnvironment<Prim>
source§impl<Prim: PrimitiveType, S, Ty> Extend<(S, Ty)> for TypeEnvironment<Prim>
impl<Prim: PrimitiveType, S, Ty> Extend<(S, Ty)> for TypeEnvironment<Prim>
source§fn extend<I: IntoIterator<Item = (S, Ty)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (S, Ty)>>(&mut self, iter: I)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl<Prim: PrimitiveType, S, Ty> FromIterator<(S, Ty)> for TypeEnvironment<Prim>
impl<Prim: PrimitiveType, S, Ty> FromIterator<(S, Ty)> for TypeEnvironment<Prim>
Auto Trait Implementations§
impl<Prim> Freeze for TypeEnvironment<Prim>
impl<Prim = Num> !RefUnwindSafe for TypeEnvironment<Prim>
impl<Prim> Send for TypeEnvironment<Prim>
impl<Prim> Sync for TypeEnvironment<Prim>
impl<Prim> Unpin for TypeEnvironment<Prim>where
Prim: Unpin,
impl<Prim = Num> !UnwindSafe for TypeEnvironment<Prim>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)