[rust-dev] Functions overloading

Niko Matsakis niko at alum.mit.edu
Thu Apr 12 06:47:12 PDT 2012


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

On 4/12/12 3:01 AM, David Rajchenbach-Teller wrote:
> On Thu Apr 12 10:08:21 2012, Alexander Stavonin wrote:
>> What's about function overloading? Something like this:
>>
>> fn foo(val: int) {
>>      io::println("int");
>> }
>>
>> fn foo(val: str) {
>>      io::println("str");
>> }
>>
>> fn main() {
>>      foo(1);
>>      foo("test");
>> }
>>
>> But :(
>>
>> main.rs:3:0: 5:1 error: duplicate definition of foo
>> main.rs:3 fn foo(val: int) {
>> main.rs:4     io::println("int");
>> main.rs:5 }
>>
>> As I understood, similar code can be implemented by using ifase and impl, but it's very cumbersome way.
> My personal experience is that you want function overloading to be very
> explicit. Having too much overloading makes it much easier to misread
> code.
>
> So, I personally would not advocate C++-style function overloading.
>
> --
> David Rajchenbach-Teller, PhD
>   Performance Team, Mozilla
>
>
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev



More information about the Rust-dev mailing list