News for hackers. Stuff that matters.
Re:Abstraction without overhead: traits in Rust by detrino on Monday May 11, 2015 @04:10PM (#9527508)

    >- allows traits to be implemented for existing types, as opposed
    >  to C++ where the type's declaration has to list all base classes.

    >- allows a type to be used through dynamic dispatch while allowing users
    >  who don't need this to avoid the vtable overhead.
In C++ you can have this as well. For example, std::function is able to wrap any callable type without them having to have any base classes. Sean Parent gives a great talk about this: http://channel9.msdn.com/Events/GoingNative/2013/Inheritance...
Re:Abstraction without overhead: traits in Rust by comex on Monday May 11, 2015 @07:10PM (#9528459)
This only works for callables, though; if you want a virtual dispatch wrapper for some other set of methods, you have to write it yourself (right?). On the other hand, if you do have a relevant base class and you know the exact type, you can avoid virtual calls in C++ just by declaring the relevant methods final, although the vtable pointer will still bloat the struct a bit.

However, I'd say Rust traits are more elegant in unifying what are two separate worlds in C++:

(1) calling virtual methods on a base class pointer, which can be (usually is) dispatched at runtime, but thus can't support methods with generic parameters or having a 'virtual type' (instead of a method), and

(2) accessing members of template parameter classes, including (possibly generic) methods, constants, typedefs, etc. - much more flexible, but doesn't work at runtime. Also dynamically typed, for better or worse; Rust thinks worse.

In Rust, (1) is a trait object, and (2) can be done with a generic parameter specified to implement a certain trait with methods, associated types, and constants. Rust's compiler doesn't try to be magic (unlike, say, Haskell), and so traits with generic methods and such can't be made into trait objects, but they're still traits - they feel like the same basic kind of thing.

Re:Abstraction without overhead: traits in Rust by humanrebar on Monday May 11, 2015 @04:25PM (#9527606)
C++ is Turing-complete, so you can do whatever you want. What Rust offers is an hierarchy free (i.e., no inheritance) polymorphism mechanism as a core part of the language.
Re:Abstraction without overhead: traits in Rust by detrino on Monday May 11, 2015 @04:29PM (#9527624)
The turing-complete argument makes no sense here.
Re:Abstraction without overhead: traits in Rust by eli_gottlieb on Monday May 11, 2015 @10:07PM (#9529165)
I think he means that, through templates, the C++ type system is Turing-complete.
Re:Abstraction without overhead: traits in Rust by steveklabnik on Monday May 11, 2015 @10:34PM (#9529246)
Rust's is too.
Re:Abstraction without overhead: traits in Rust by kibwen on Monday May 11, 2015 @11:45PM (#9529488)
Though the skimpy recursion limit makes it fairly impossible to do anything interesting with this property, much to the chagrin of some people. :P
Re:Abstraction without overhead: traits in Rust by humanrebar on Monday May 11, 2015 @05:21PM (#9527939)
Sure it does. I can pass around a struct containing any bytes I want and do anything I want with it. If I want to define FatPointer that contains an opaque pointer (or some equivalent union) and a polymorphic Strategy object of some sort, I can.

You can do the same in C, for that matter. Or in assembly.

The reason we still come up with new languages is so that you can write code that conforms to certain patterns easily.

Re:Abstraction without overhead: traits in Rust by geofft on Monday May 11, 2015 @07:12PM (#9528470)
No, Turing-completeness does not mean you can "do whatever you want". It means you can compute whatever you want that is computable. If the way you want to compute things is to embed a Rust compiler, yes you can do that, but there are many things that we'd colloquially refer to as "do whatever you want" that apply to the semantics of C++ itself.

You cannot, for instance, create a new abstract base class and have a pre-existing class in someone else's header implement your abstract base class, such that a pointer to that pre-existing class can be dynamic_cast to your class.

Because that statement refers to C++ semantics, not your ability to compute things, it's not affected by the claim of Turing-completeness.

Re:Abstraction without overhead: traits in Rust by humanrebar on Monday May 11, 2015 @09:04PM (#9528919)
> You cannot, for instance, create a new abstract base class and have a pre-existing class in someone else's header implement your abstract base class, such that a pointer to that pre-existing class can be dynamic_cast to your class.

Sure you can, for some value of base class and cast. You might not be able to use particular keywords, but data is data and math is math. You might not be able to use the built-in type system to do it.

C and javascript programmers write their own inheritance-based type systems all the time. And anything you can do in C, you can do in C++ if you really want to.

Re:Abstraction without overhead: traits in Rust by marrs on Tuesday May 12, 2015 @08:15AM (#9531132)
JS is a bad example because people who do that are preferring inheritance to composition, which is absurd.

In the case of C, yes you can do that, and where performance is critical you might prefer to do it, but it's obviously not going to be your first choice if you have any sense.

Re:Abstraction without overhead: traits in Rust by jahewson on Monday May 11, 2015 @05:36PM (#9528021)
That has nothing to do with Turing-completeness. There are Turing-complete languages which don't even have pointers.