[rust-dev] Functions overloading

Alexander Stavonin a.stavonin at gmail.com
Thu Apr 12 21:30:46 PDT 2012


The closest variant can be implemented by using enums, but I can't say that
resulted code is nice.:

enum foo_param {
    i(int),
    s(str)
}

fn foo(param: foo_param) {
    alt param {
        i(int_val) {
            io::println(#fmt("foo was called with int == %d", int_val));
        }
        s(str_val) {
            io::println(#fmt("foo was called with str == %s", str_val));
        }
    }
}

fn main() {
    foo(i(10));
    foo(s("test"));
}


Not only that, but it adds an extra layer of complexity for type
> inferencing, which quite frankly is already complex enough.
>
> That said, you can do a limited form of overloading using impls:
>
> impl methods for int {
>    fn foo() { ... }
> }
>
> impl methods for uint {
>    fn foo() { ... }
> }
>
>
>
>
> Niko
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20120413/24ab9155/attachment.html>


More information about the Rust-dev mailing list