Enum arithmetic_typing::Type

source ·
#[non_exhaustive]
pub enum Type<Prim: PrimitiveType = Num> { Any, Dyn(DynConstraints<Prim>), Prim(Prim), Function(Box<Function<Prim>>), Tuple(Tuple<Prim>), Object(Object<Prim>), Var(TypeVar), }
Expand description

Enumeration encompassing all types supported by the type system.

Parametric by the PrimitiveType.

§Notation

§Examples

There are conversions to construct Types eloquently:

let tuple: Type = (Type::BOOL, Type::NUM).into();
assert_eq!(tuple.to_string(), "(Bool, Num)");
let slice = tuple.repeat(UnknownLen::param(0));
assert_eq!(slice.to_string(), "[(Bool, Num); N]");
let fn_type: Type = Function::builder()
    .with_arg(slice)
    .returning(Type::NUM)
    .into();
assert_eq!(fn_type.to_string(), "([(Bool, Num); N]) -> Num");

A Type can also be parsed from a string:

let slice = <Type>::try_from(&TypeAst::try_from("[(Bool, Num)]")?)?;
assert_matches!(slice, Type::Tuple(t) if t.as_slice().is_some());
let fn_type = <Type>::try_from(&TypeAst::try_from("([(Bool, Num); N]) -> Num")?)?;
assert_matches!(fn_type, Type::Function(_));

§Any type

Self::Any, denoted as any, is a catch-all type similar to any in TypeScript. It allows to circumvent type system limitations at the cost of being extremely imprecise. any type can be used in any context (destructured, called with args of any quantity and type and so on), with each application of the type evaluated independently. Thus, the same any variable can be treated as a function, a tuple, a primitive type, etc.


let code = "
    wildcard: any = 1; // `any` can be assigned from anything
    wildcard == 1 && wildcard == (2, 3);
    (x, y, ...) = wildcard; // destructuring `any` always succeeds
    wildcard(1, |x| x + 1); // calling `any` as a function works as well
";
let ast = Annotated::<F32Grammar>::parse_statements(code)?;
let mut env = TypeEnvironment::new();
env.process_statements(&ast)?;

// Destructure outputs are certain types that can be inferred
// from their usage, rather than `any`!
assert_matches!(env["x"], Type::Var(_));
let bogus_code = "x + 1 == 2; x(1)";
let ast = Annotated::<F32Grammar>::parse_statements(bogus_code)?;
let errors = env.process_statements(&ast).unwrap_err();
let err = errors.iter().next().unwrap();
assert_eq!(err.main_location().span(bogus_code), "x(1)");

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Any

Any type aka “I’ll think about typing later”. Similar to any type in TypeScript. See the dedicated section for more details.

§

Dyn(DynConstraints<Prim>)

Arbitrary type implementing certain constraints. Similar to dyn _ types in Rust or use of interfaces in type position in TypeScript.

See DynConstraints for details.

§

Prim(Prim)

Primitive type.

§

Function(Box<Function<Prim>>)

Functional type.

§

Tuple(Tuple<Prim>)

Tuple type.

§

Object(Object<Prim>)

Object type.

§

Var(TypeVar)

Type variable.

Implementations§

source§

impl Type

source

pub const NUM: Self = _

Numeric primitive type.

source§

impl<Prim: WithBoolean> Type<Prim>

source

pub const BOOL: Self = _

Boolean primitive type.

source§

impl<Prim: PrimitiveType> Type<Prim>

source

pub fn void() -> Self

Returns a void type (an empty tuple).

source

pub fn param(index: usize) -> Self

Creates a bounded type variable with the specified index.

source

pub fn slice( element: impl Into<Type<Prim>>, length: impl Into<TupleLen>, ) -> Self

Creates a slice type.

source

pub fn repeat(self, length: impl Into<TupleLen>) -> Slice<Prim>

Creates a slice type by repeating this type.

source

pub fn is_void(&self) -> bool

Checks if this type is void (i.e., an empty tuple).

source

pub fn is_concrete(&self) -> bool

Returns true iff this type does not contain type / length variables.

See TypeEnvironment for caveats of dealing with non-concrete types.

Trait Implementations§

source§

impl<Prim: Clone + PrimitiveType> Clone for Type<Prim>

source§

fn clone(&self) -> Type<Prim>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<Prim: Debug + PrimitiveType> Debug for Type<Prim>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Prim: PrimitiveType> Display for Type<Prim>

source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Prim> From<()> for Type<Prim>
where Prim: PrimitiveType,

source§

fn from(tuple: ()) -> Self

Converts to this type from the input type.
source§

impl<Prim, T: Into<Type<Prim>>> From<(T,)> for Type<Prim>
where Prim: PrimitiveType,

source§

fn from(tuple: (T,)) -> Self

Converts to this type from the input type.
source§

impl<Prim, T: Into<Type<Prim>>, U: Into<Type<Prim>>> From<(T, U)> for Type<Prim>
where Prim: PrimitiveType,

source§

fn from(tuple: (T, U)) -> Self

Converts to this type from the input type.
source§

impl<Prim, T: Into<Type<Prim>>, U: Into<Type<Prim>>, V: Into<Type<Prim>>> From<(T, U, V)> for Type<Prim>
where Prim: PrimitiveType,

source§

fn from(tuple: (T, U, V)) -> Self

Converts to this type from the input type.
source§

impl<Prim, T: Into<Type<Prim>>, U: Into<Type<Prim>>, V: Into<Type<Prim>>, W: Into<Type<Prim>>> From<(T, U, V, W)> for Type<Prim>
where Prim: PrimitiveType,

source§

fn from(tuple: (T, U, V, W)) -> Self

Converts to this type from the input type.
source§

impl<Prim, T: Into<Type<Prim>>, U: Into<Type<Prim>>, V: Into<Type<Prim>>, W: Into<Type<Prim>>, X: Into<Type<Prim>>> From<(T, U, V, W, X)> for Type<Prim>
where Prim: PrimitiveType,

source§

fn from(tuple: (T, U, V, W, X)) -> Self

Converts to this type from the input type.
source§

impl<Prim, T: Into<Type<Prim>>, U: Into<Type<Prim>>, V: Into<Type<Prim>>, W: Into<Type<Prim>>, X: Into<Type<Prim>>, Y: Into<Type<Prim>>> From<(T, U, V, W, X, Y)> for Type<Prim>
where Prim: PrimitiveType,

source§

fn from(tuple: (T, U, V, W, X, Y)) -> Self

Converts to this type from the input type.
source§

impl<Prim, T: Into<Type<Prim>>, U: Into<Type<Prim>>, V: Into<Type<Prim>>, W: Into<Type<Prim>>, X: Into<Type<Prim>>, Y: Into<Type<Prim>>, Z: Into<Type<Prim>>> From<(T, U, V, W, X, Y, Z)> for Type<Prim>
where Prim: PrimitiveType,

source§

fn from(tuple: (T, U, V, W, X, Y, Z)) -> Self

Converts to this type from the input type.
source§

impl<Prim, T: Into<Type<Prim>>, U: Into<Type<Prim>>, V: Into<Type<Prim>>, W: Into<Type<Prim>>, X: Into<Type<Prim>>, Y: Into<Type<Prim>>, Z: Into<Type<Prim>>, A: Into<Type<Prim>>> From<(T, U, V, W, X, Y, Z, A)> for Type<Prim>
where Prim: PrimitiveType,

source§

fn from(tuple: (T, U, V, W, X, Y, Z, A)) -> Self

Converts to this type from the input type.
source§

impl<Prim, T: Into<Type<Prim>>, U: Into<Type<Prim>>, V: Into<Type<Prim>>, W: Into<Type<Prim>>, X: Into<Type<Prim>>, Y: Into<Type<Prim>>, Z: Into<Type<Prim>>, A: Into<Type<Prim>>, B: Into<Type<Prim>>> From<(T, U, V, W, X, Y, Z, A, B)> for Type<Prim>
where Prim: PrimitiveType,

source§

fn from(tuple: (T, U, V, W, X, Y, Z, A, B)) -> Self

Converts to this type from the input type.
source§

impl<Prim, T: Into<Type<Prim>>, U: Into<Type<Prim>>, V: Into<Type<Prim>>, W: Into<Type<Prim>>, X: Into<Type<Prim>>, Y: Into<Type<Prim>>, Z: Into<Type<Prim>>, A: Into<Type<Prim>>, B: Into<Type<Prim>>, C: Into<Type<Prim>>> From<(T, U, V, W, X, Y, Z, A, B, C)> for Type<Prim>
where Prim: PrimitiveType,

source§

fn from(tuple: (T, U, V, W, X, Y, Z, A, B, C)) -> Self

Converts to this type from the input type.
source§

impl<Prim: WithBoolean> From<Assertions> for Type<Prim>

source§

fn from(value: Assertions) -> Self

Converts to this type from the input type.
source§

impl<Prim: PrimitiveType> From<DynConstraints<Prim>> for Type<Prim>

source§

fn from(constraints: DynConstraints<Prim>) -> Self

Converts to this type from the input type.
source§

impl<Prim: PrimitiveType> From<FnWithConstraints<Prim>> for Type<Prim>

source§

fn from(value: FnWithConstraints<Prim>) -> Self

Converts to this type from the input type.
source§

impl<Prim: PrimitiveType> From<Function<Prim>> for Type<Prim>

source§

fn from(fn_type: Function<Prim>) -> Self

Converts to this type from the input type.
source§

impl<Prim: PrimitiveType> From<Object<Prim>> for Type<Prim>

source§

fn from(object: Object<Prim>) -> Self

Converts to this type from the input type.
source§

impl<Prim: WithBoolean> From<Prelude> for Type<Prim>

source§

fn from(value: Prelude) -> Self

Converts to this type from the input type.
source§

impl<Prim: PrimitiveType> From<Slice<Prim>> for Type<Prim>

source§

fn from(slice: Slice<Prim>) -> Self

Converts to this type from the input type.
source§

impl<Prim: PrimitiveType> From<Tuple<Prim>> for Type<Prim>

source§

fn from(tuple: Tuple<Prim>) -> Self

Converts to this type from the input type.
source§

impl<Prim: PrimitiveType> PartialEq for Type<Prim>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, Prim: PrimitiveType> TryFrom<&LocatedSpan<&'a str, TypeAst<'a>>> for Type<Prim>

§

type Error = Errors<Prim>

The type returned in the event of a conversion error.
source§

fn try_from(ast: &SpannedTypeAst<'a>) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<Prim> Freeze for Type<Prim>
where Prim: Freeze,

§

impl<Prim = Num> !RefUnwindSafe for Type<Prim>

§

impl<Prim> Send for Type<Prim>

§

impl<Prim> Sync for Type<Prim>

§

impl<Prim> Unpin for Type<Prim>
where Prim: Unpin,

§

impl<Prim = Num> !UnwindSafe for Type<Prim>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.