Earlier quoted context omitted.
So in Rust traits can include full methods bodies, not only signatures?
Yes. The feature has been proposed for Java too: "defender methods". Originally I resisted them on the grounds of not being necessary, but they're used all over now. Being able to supply a default implementation is extremely useful.
Abstraction without overhead: traits in Rust
31–40 of 150 posts
Re: Abstraction without overhead: traits in Rust
#32> Traits are interfaces So why not use "interface" keyword?
They aren't really interfaces. They can be like interfaces (just method signatures) or like classes without data (including definitions for some or all methods). Using a name distinct from either "class" or "interface" limits the degree to which incorrect expectations from similar-but-critically-different constructs in other language interfere with understanding of the Rust construct.
Re: Abstraction without overhead: traits in Rust
#33> Traits are interfaces So why not use "interface" keyword?
Probably for the same reason that they call their unions "enums" or their heap blocks "boxes". Standardization of jargon has never been a thing with rust, just roll with it.
Re: Abstraction without overhead: traits in Rust
#34Earlier quoted context omitted.
So in Rust traits can include full methods bodies, not only signatures?
Yes. The feature has been proposed for Java too: "defender methods". Originally I resisted them on the grounds of not being necessary, but they're used all over now. Being able to supply a default implementation is extremely useful.
Re: Abstraction without overhead: traits in Rust
#35Earlier quoted context omitted.
Probably for the same reason that they call their unions "enums" or their heap blocks "boxes". Standardization of jargon has never been a thing with rust, just roll with it.
Well, enums aren't unions, they're tagged unions. And other languages call boxed values by that name too.
"Enumerate" in English is just a fancy word for "count" -- it means to assign numbers to a bunch of things. Which is exactly what the C concept did. The Rust usage (to mean "something that can be in one of a few different states") is new, though Java has something fairly close too.
It was a poor choice, sorry. Likewise being deliberately difficult with "trait" vs. "interface" (picking Self's jargon instead of the term that literally everyone already knows from decades of OOP) didn't serve you well. Thus we have blog posts like this needing to tell us what we probably should have been able to figure out from context.
Finally, regarding "box" vs. "block". Other languages (C# is the only one that comes to mind off-hand) have used the idea of "boxing" to imply the allocation of space for and copying of pass-by-reference data. That's sort of a different notion than simple heap allocation, so it sort of gets its own jargon I guess. I didn't complain anyway. But with Rust, a "box" really is used to refer to a dynamic heap block in any context. We sort of already had a perfectly good word for that.
Pretentious jargon isn't the worst crime in the world, but I do think Rust seems needlessly complicated in the way it likes to play Shakespeare with existing concepts.
Re: Abstraction without overhead: traits in Rust
#36Earlier quoted context omitted.
Well, enums aren't unions, they're tagged unions. And other languages call boxed values by that name too.
Stop it, they're unions. :) Among the target market, people are going to intimately familiar with the use of "enum" and "union" from the C language. Rust's concept of a single object that can store exactly one of several types of sub-objects matches one quite closely, and not the other. Having a runtime tag and affirmative checking doesn't change the nature of the thing. We don't call "cars" something different when…
Re: Abstraction without overhead: traits in Rust
#37> Traits are interfaces So why not use "interface" keyword?
> So why not use "interface" keyword? They aren't really interfaces. They can be like interfaces (just method signatures) or like classes without data (including definitions for some or all methods). Using a name distinct from either "class" or "interface" limits the degree to which incorrect expectations from similar-but-critically-different constructs in other language interfere with understanding of the Rust const…
Re: Abstraction without overhead: traits in Rust
#38Great article. The main bit of new information for me was that while Rust supports dynamic dispatch, its implementation has a noticeable difference from C++. In C++, the vtable pointer is in the object itself. In Rust, it's stored inside what is essentially a "fat pointer." Pointers to traits ("trait objects") are actually two pointers: the pointer to the vtable and the pointer to the actual object. This seems to hav…
Though the added complexity could offset the 16-to-8 byte size reduction.
Re: Abstraction without overhead: traits in Rust
#39> Traits are interfaces So why not use "interface" keyword?
> Traits are somewhat between an interface and a mixin: an interface is made only of method signatures, while a trait includes also the full method definitions, on the other side mixins include method definitions, but they can also carry state through attributes while traits usually don't. Rust's traits may specify just a method signature and force the implementor of the trait to implement the method, but they may sp…
Re: Abstraction without overhead: traits in Rust
#40Earlier quoted context omitted.
Well, enums aren't unions, they're tagged unions. And other languages call boxed values by that name too.
Stop it, they're unions. :) Among the target market, people are going to intimately familiar with the use of "enum" and "union" from the C language. Rust's concept of a single object that can store exactly one of several types of sub-objects matches one quite closely, and not the other. Having a runtime tag and affirmative checking doesn't change the nature of the thing. We don't call "cars" something different when…