Good points, Patrick. Added Generics to wiki page for Cxx programmers (probably could use more info there also) https://github.com/mozilla/rust/wiki/Rust-for-CXX-programmers
On Thu, Apr 4, 2013 at 5:30 PM, Patrick Walton <[email protected]> wrote: > On 4/4/13 2:45 PM, Jeaye Wilkerson wrote: > >> 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<http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error>) >> approach. >> > > Rust generics are more like C++13 templates with concepts, not like the > templates that exist today. This allows templates to be typechecked at the > time you write them, not when you instantiate them. You can use macros for > the more unstructured 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(). >> > > This is something that Niko addresses in these posts: > > http://smallcultfollowing.com/**babysteps/blog/2013/04/02/** > associated-items/<http://smallcultfollowing.com/babysteps/blog/2013/04/02/associated-items/> > > http://smallcultfollowing.com/**babysteps/blog/2013/04/03/** > associated-items-continued/<http://smallcultfollowing.com/babysteps/blog/2013/04/03/associated-items-continued/> > > Allowing `T::zero()` could be done and has advantages and disadvantages, > but I've shied away from it so far, because of the problems with types that > aren't paths (`<~[int]>::empty()`) and ambiguities (what if there are two > traits that define `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? >> > > You would need another bound on the function. Or just use the Num trait, > as below. > > > Another problem that arises is the massive number of >> trait bounds that an arithmetic (or something else) function might need >> to simply work. >> > > This is because trait inheritance is currently broken. Once it's fixed, > you'll just be able to use the Num trait and you'll get all of the > arithmetic with one short trait bound. > > Patrick > > ______________________________**_________________ > Rust-dev mailing list > [email protected] > https://mail.mozilla.org/**listinfo/rust-dev<https://mail.mozilla.org/listinfo/rust-dev> > -- -Thad http://www.freebase.com/view/en/thad_guidry
_______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
