pub struct AlgebraicDataType { /* private fields */ }Expand description
A tagged-union type: a named set of variants, each with its own fields.
The kernel defines the Shape match/binding protocol; this type is the
pattern organ’s concrete declaration of an algebraic data type. It produces
VariantConstructors for building tagged values and a kernel Shape
(AdtShape) for matching them. See the crate README for the
protocol-versus-behavior boundary.
§Examples
use std::sync::Arc;
use sim_kernel::Symbol;
use sim_lib_pattern::{AlgebraicDataType, VariantDeclaration};
let maybe = AlgebraicDataType::new(
Symbol::qualified("adt", "Maybe"),
vec![
VariantDeclaration::nullary(Symbol::qualified("maybe", "Nothing")),
VariantDeclaration::nullary(Symbol::qualified("maybe", "Just")),
],
)
.unwrap();
assert_eq!(maybe.constructors().len(), 2);
assert!(maybe.constructor(&Symbol::qualified("maybe", "Just")).is_some());Implementations§
Source§impl AlgebraicDataType
impl AlgebraicDataType
Sourcepub fn new(symbol: Symbol, variants: Vec<VariantDeclaration>) -> Result<Self>
pub fn new(symbol: Symbol, variants: Vec<VariantDeclaration>) -> Result<Self>
Builds an ADT from its name and variants.
§Errors
Returns an error if two variants share a tag symbol.
Sourcepub fn variants(&self) -> impl Iterator<Item = &VariantDeclaration>
pub fn variants(&self) -> impl Iterator<Item = &VariantDeclaration>
Iterates the declared variants in tag order.
Sourcepub fn constructor(&self, variant: &Symbol) -> Option<VariantConstructor>
pub fn constructor(&self, variant: &Symbol) -> Option<VariantConstructor>
Returns the constructor for variant, or None if it is not declared.
Sourcepub fn constructors(&self) -> Vec<VariantConstructor>
pub fn constructors(&self) -> Vec<VariantConstructor>
Returns a constructor for every declared variant.
Trait Implementations§
Source§impl Clone for AlgebraicDataType
impl Clone for AlgebraicDataType
Source§fn clone(&self) -> AlgebraicDataType
fn clone(&self) -> AlgebraicDataType
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for AlgebraicDataType
impl !UnwindSafe for AlgebraicDataType
impl Freeze for AlgebraicDataType
impl Send for AlgebraicDataType
impl Sync for AlgebraicDataType
impl Unpin for AlgebraicDataType
impl UnsafeUnpin for AlgebraicDataType
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
Mutably borrows from an owned value. Read more