3 ms·
Cool stuff. I've also seen generic traits and tuples used to fake function overloading with different arity: trait Foo<T> { fn foo(&self, other: T); }
by xscott 5y ago
Cool stuff.
I've also seen generic traits and tuples used to fake function overloading with different arity:
trait Foo<T> { fn foo(&self, other: T); }
impl Foo<()> for &str {
fn foo(&self, _other: ()) { print!("0 params\n"); } }
impl Foo<(f64,)> for &str {
fn foo(&self, _other: (f64,)) { print!("1 params\n"); } }
impl Foo<(f64, f64)> for &str {
fn foo(&self, _other: (f64, f64)) { print!("2 params\n"); } }
fn main() {
"".foo(()); // pretend that's 0 args
"".foo((1.0,)); // pretend that's 1 args
"".foo((1.0, 2.0)); // pretend that's 2 args
}
Not quite as pretty, but it works, and you can be generic or specific over each sub-item in the tuple.