Howdy,

I've been tinkering with Rust generics lately and, unfortunately, they seem 
very limiting. Coming from C++, I expect from generics what templates provide: 
A plug-in-this-type-in-and-see-if-it-compiles ( 
http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error ) approach. 
It seems to me that trait-based generics are not really generic at all; they're 
polymorphic. Take the following:

impl<T: num::Zero> Foo
{
        pub fn zero() -> Foo<T>
        {
                Foo { bar: num::Zero::zero() }
        }
}

Often times in C++, a programmer will want to restrict the types that can go 
into a function. C++ traits make this kind of messy, so I appreciate the 
approach here. Still, getting zero from T uses a polymorphic num::Zero::zero(), 
instead of a generic T::zero(). Furthermore, what if one wanted to create also 
a "pub fn one() -> Foo<T>" function; you then need "num::Zero::zero() + 1", but 
does that work as expected? Another problem that arises is the massive number 
of trait bounds that an arithmetic (or something else) function might need to 
simply work. 

Rust is doing an excellent job addressing a number of crucial problems of C++, 
but I feel it broke one thing C++ did pretty well. For the 0.6 release, it was 
said that there are no plans for major syntactic changes; are there no plans to 
cleanup Rust generics? Are there cleaner ways of approaching the problems I'm 
running into as I try to develop generic types in Rust? Perhaps this can be 
made sexier with macros?

Cheers,
Jeaye
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to