trait definition and struct-tuple example

This commit is contained in:
vegowotenks 2025-05-02 19:38:30 +02:00
commit 1a43c2d1a5
7 changed files with 112 additions and 0 deletions

31
src/generic.rs Normal file
View file

@ -0,0 +1,31 @@
pub struct Sum<Con, Rhs> where Con: IsConType, Rhs: IsRepType {
pub left: Con,
pub right: Rhs,
}
pub struct Product<T, Rhs> where Rhs: IsConType {
pub left: Field<T>,
pub right: Rhs
}
pub struct Field<T> {
pub value: T
}
// types used for generic representation
pub trait IsRepType {}
impl<Lhs: IsConType, Rhs: IsRepType> IsRepType for Sum<Lhs, Rhs> {}
impl<T: IsRepType, Rhs: IsConType> IsRepType for Product<T, Rhs> {}
impl<T> IsRepType for Field<T> {}
// types that represent constructors
pub trait IsConType {}
impl<T, Rhs: IsConType> IsConType for Product<T, Rhs> {}
impl<T> IsConType for Field<T> {}
pub trait Generic {
type Representation: IsRepType;
fn generalize(self) -> Self::Representation;
fn specialize(rep: Self::Representation) -> Self;
}