[rust-dev] Functions overloading

Alexander Stavonin a.stavonin at gmail.com
Wed Apr 18 16:26:20 PDT 2012


Stefan, I understood you idea but I have problem with compilation.

fn_overloading.rs:31:34: 31:80 error: method `to_input` has an incompatible
type: type parameter vs int
fn_overloading.rs:31 impl <t> of to_input<t> for int { fn to_input() ->
input<int> { ret val(self); } }

 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Also, am I right that in this case we could not make overloading for
different types, for example for str and int?

Alexander.


I was reading this again and thought that as a middle ground between type
> classes and enums, one could use type classes that wrap supported types
> into enum variants
> for easily switching over them.
>
> enum input<t> { val(t), seq([t]) }
>
> iface to_input<t> { fn to_input() -> input<t> }
> impl <t> of to_input<t> for int { fn to_input() -> input<int> { ret
> val(self); } }
> impl <t> of to_input<t> for [int] { fn to_input() -> input<int> { ret
> seq(self); } }
>
> fn to_input<t>(i: to_input<t>) {
> alt i.to_input() {
>  val(v) { ... }
>  seq(v) { ... }
> }
> }
>
> This way the user does not have to know about the internal enums and the
> implementor can simply switch (especially useful with multiple arguments).
>
> I think it would be nice too have some syntax support for building ifaces
> like that, perhaps via macros.
>
> -- Stefan
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20120419/ebed4480/attachment-0001.html>


More information about the Rust-dev mailing list