polymorphism

Macro polymorphism 

Source
polymorphism!() { /* proc-macro */ }
Expand description

§Polymorphism

A procedural macro to imitate polymorphism which can be seen and found in many modern programming languages. Can be used similarly to an fn or impl declaration, but polymorphism allows for duplicate fn names with different signitures (types as parameters). This implementation of polymorphism bypasses the orphan rule with a Local type.

§Examples

§- Init:

To use polymorphism! it needs to be initialised. This can be done with a simple

polymorphism!();

closure. Don’t initialise twice!

§- Fn Declaration:

polymorphism!(
    pub fn func(n: i32, m: i32) -> i32 {
        n+m
    }
    pub fn func(n: f64, m: f64) -> f64 {
        n-m
    }
);

§- Fn Usage:

assert_eq!(polymorphism!(func(1,2)), 3);
assert_eq!(polymorphism!(func(1.0,2.0)), -1.0);

§- Impl Declaration:

polymorphism!(
    impl<T> Vec<T> {
        pub fn add_elem(&mut self, elem: T) {
            self.push(elem);
        }
        pub fn add_elem(&mut self, mut elems: Vec<T>) {
            self.append(&mut elems);
        }
    }
);

§- Impl Usage:

let mut v=vec![1,2,3];
polymorphism!(v.add_elem(4));
polymorphism!(v.add_elem(vec![5,6,7]));
assert_eq!(v,vec![1,2,3,4,5,6,7]);

§- Impl Struct Method Declaration:

polymorphism!(
    impl<T> Vec<T> {
        pub fn new() -> Self {
            Vec::new()
        }
        pub fn new<const N: usize>(arr: [T;N]) -> Self {
            Vec::from(arr)
        }
    }
);

§- Impl Struct Method Usage:

let mut v: Vec<i32>=vec![];
assert_eq!(polymorphism!(Vec<i32>::new()),v);
v.extend([1,2,3]);
assert_eq!(polymorphism!(Vec<i32>::new([1,2,3])),v);

§- polymorphism From Crate:

polymorphism!(crate_name: func(1,2));
polymorphism!(crate_name: v.add_elem(4));
polymorphism!(crate_name: Vec<i32>::new([1,2,3]));

§Notes:

  • You may need to add lifetime specifiers to references (such as & or &mut)
  • When using impl declaration, functions with the same name should be declared in the same polymorphism! closure
  • Traits don’t work on impl declarations using polymorphism!
  • All impl functions with the same name must have the same reference to self (&self, &mut self or self)