Struct arithmetic_typing::Object

source ·
pub struct Object<Prim: PrimitiveType> { /* private fields */ }
Expand description

Object type: a collection of named fields with heterogeneous types.

§Notation

Object types are denoted using a brace notation such as { x: Num, y: [(Num, 'T)] }. Here, x and y are field names, and Num / [(Num, 'T)] are types of the corresponding object fields.

§As constraint

Object types are exact; their extensions cannot be unified with the original types. For example, if a function argument is { x: Num, y: Num }, the function cannot be called with an arg of type { x: Num, y: Num, z: Num }:

let code = "
    sum_coords = |pt: { x: Num, y: Num }| pt.x + pt.y;
    sum_coords(#{ x: 3, y: 4 }); // OK
    sum_coords(#{ x: 3, y: 4, z: 5 }); // fails
";
let ast = Annotated::<F32Grammar>::parse_statements(code)?;
let err = TypeEnvironment::new().process_statements(&ast).unwrap_err();
let err = err.iter().next().unwrap();
assert_matches!(err.kind(), ErrorKind::FieldsMismatch { .. });

To bridge this gap, objects can be used as a constraint on types, similarly to Constraints. As a constraint, an object specifies necessary fields, which can be arbitrarily extended.

The type inference algorithm uses object constraints, not concrete object types whenever possible:

let code = "
    sum_coords = |pt| pt.x + pt.y;
    sum_coords(#{ x: 3, y: 4 }); // OK
    sum_coords(#{ x: 3, y: 4, z: 5 }); // also OK
";
let ast = Annotated::<F32Grammar>::parse_statements(code)?;
let mut env = TypeEnvironment::new();
env.process_statements(&ast)?;
assert_eq!(
    env["sum_coords"].to_string(),
    "for<'T: { x: 'U, y: 'U }, 'U: Ops> ('T) -> 'U"
);

Note that the object constraint in this case refers to another type param, which is constrained on its own!

Implementations§

source§

impl<Prim: PrimitiveType> Object<Prim>

source

pub fn new() -> Self

Creates an empty object.

source

pub fn get(&self, name: &str) -> Option<&Type<Prim>>

Returns type of a field with the specified name.

source

pub fn iter(&self) -> impl Iterator<Item = (&str, &Type<Prim>)> + '_

Iterates over fields in this object.

source

pub fn field_names(&self) -> impl Iterator<Item = &str> + '_

Iterates over field names in this object.

source

pub fn into_dyn(self) -> Type<Prim>

Converts this object into a corresponding dynamic constraint.

Trait Implementations§

source§

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

source§

fn clone(&self) -> Object<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 Object<Prim>

source§

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

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

impl<Prim: PrimitiveType> Default for Object<Prim>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

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

source§

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

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

impl<Prim, S, V, const N: usize> From<[(S, V); N]> for Object<Prim>
where Prim: PrimitiveType, S: Into<String>, V: Into<Type<Prim>>,

source§

fn from(entries: [(S, V); N]) -> Self

Converts to this type from the input type.
source§

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

source§

fn from(object: Object<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, S, V> FromIterator<(S, V)> for Object<Prim>
where Prim: PrimitiveType, S: Into<String>, V: Into<Type<Prim>>,

source§

fn from_iter<T: IntoIterator<Item = (S, V)>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<Prim: PrimitiveType> Index<&str> for Object<Prim>

§

type Output = Type<Prim>

The returned type after indexing.
source§

fn index(&self, field_name: &str) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<Prim: PartialEq + PrimitiveType> PartialEq for Object<Prim>

source§

fn eq(&self, other: &Object<Prim>) -> 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<Prim: PrimitiveType> StructuralPartialEq for Object<Prim>

Auto Trait Implementations§

§

impl<Prim> Freeze for Object<Prim>

§

impl<Prim> !RefUnwindSafe for Object<Prim>

§

impl<Prim> Send for Object<Prim>

§

impl<Prim> Sync for Object<Prim>

§

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

§

impl<Prim> !UnwindSafe for Object<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.