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 Constraint
s.
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>
impl<Prim: PrimitiveType> Object<Prim>
sourcepub fn get(&self, name: &str) -> Option<&Type<Prim>>
pub fn get(&self, name: &str) -> Option<&Type<Prim>>
Returns type of a field with the specified name
.
sourcepub fn iter(&self) -> impl Iterator<Item = (&str, &Type<Prim>)> + '_
pub fn iter(&self) -> impl Iterator<Item = (&str, &Type<Prim>)> + '_
Iterates over fields in this object.
sourcepub fn field_names(&self) -> impl Iterator<Item = &str> + '_
pub fn field_names(&self) -> impl Iterator<Item = &str> + '_
Iterates over field names in this object.
Trait Implementations§
source§impl<Prim: PrimitiveType> Default for Object<Prim>
impl<Prim: PrimitiveType> Default for Object<Prim>
source§impl<Prim: PrimitiveType> Display for Object<Prim>
impl<Prim: PrimitiveType> Display for Object<Prim>
source§impl<Prim: PrimitiveType> From<Object<Prim>> for DynConstraints<Prim>
impl<Prim: PrimitiveType> From<Object<Prim>> for DynConstraints<Prim>
source§impl<Prim, S, V> FromIterator<(S, V)> for Object<Prim>
impl<Prim, S, V> FromIterator<(S, V)> for Object<Prim>
source§impl<Prim: PartialEq + PrimitiveType> PartialEq for Object<Prim>
impl<Prim: PartialEq + PrimitiveType> PartialEq for Object<Prim>
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> 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
)