#overloading #syntax #macro #def #function #name #overloaded

bin+lib function_overloading

a crate that adds function overloading

2 releases

new 0.1.1 Oct 23, 2024
0.1.0 Aug 8, 2024

#1091 in Rust patterns

Download history 84/week @ 2024-08-02 25/week @ 2024-08-09 1/week @ 2024-08-16 9/week @ 2024-09-13 7/week @ 2024-09-20 4/week @ 2024-09-27 1/week @ 2024-10-04 156/week @ 2024-10-18

163 downloads per month

MIT license

5KB
64 lines

Function Overloading

This is a Rust library that adds function overloading through Rust macros. It supports normal function syntax but does not currently support advanced features like async, generics, or lifetimes.

Usage

Define overloaded functions using the def! macro:

def! {
    foo, // The name of the function
    fn (a: u32, b: u32) {
        println!("{}", a + b);
    }, // The comma is not optional
    fn (s: &str) -> () { // you dont need the return type explicitly
        println!("{}", s);
    },
    fn (s: char) -> String { // You can also have different return types
       return s.to_string();
    } // You can add a comma at the end, but it's not required
}

You can use the overloaded function by calling it as a macro:

fn main() {
    foo!(1, 2); // Outputs: 3
    foo!("bar"); // Outputs: bar
    let bar: String = foo!('a');
}

Future Plans

  • Improved Syntax: Making the syntax more like defining functions.
  • Additional Features: Adding support for generics, lifetimes, async functions, etc.
  • Proc Macros: Using proc macros to make -> () implicit.
    • turns out I didn't need a proc macro for this
  • Add to crates.io

Dependencies

~4KB