Macro timrs_hkt_macro::hkt

source ·
hkt!() { /* proc-macro */ }
Expand description

Macro for defining a Higher-Kinded Type of a specific arity.

This macro will generate a public trait with the format:

pub trait HKT{N} {
    type T1;
    ...;
    type T{N};
    type With<_T1, ..., T{N}>: ...
}

Where {N} is the arity of your Higher-Kinded Type.

The current type being stored in the generic parameter in the position N can be accessed through the associated type Self::T{N} and a new instance of a Higher-Kinded Type with types X to X{N} can be instantiated via the generic associated type Self::With<X, ..., X{N}>.

§Examples

use timrs_hkt_macro::{hkt, HKT};

/* The parameter indicates the arity of the Higher-Kinded Type trait to be generated */
hkt!(1);

trait Functor: HKT1 {
    fn map<B, F>(self, f: F) -> Self::With<B>
    where
        F: FnMut(Self::T1) -> B;
}

#[derive(Debug, PartialEq)]
enum Example1<A> {
    First(A),
}

impl<A> HKT1 for Example1<A> {
    type T1 = A;
    type With<_T1> = Example1<_T1>;
}

impl<A> Functor for Example1<A> {
    fn map<B, F>(self, mut f: F) -> Self::With<B>
    where
        F: FnMut(Self::T1) -> B,
    {
        match self {
            Self::First(a) => Example1::First(f(a)),
        }
    }
}

assert_eq!(
    Example1::First("The magic number is 42".to_owned()),
    Example1::First(42).map(|x| "The magic number is ".to_owned() + &x.to_string())
);