Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 8 - confused about example at the end #38

Open
orvly opened this issue May 14, 2017 · 4 comments
Open

Chapter 8 - confused about example at the end #38

orvly opened this issue May 14, 2017 · 4 comments

Comments

@orvly
Copy link

orvly commented May 14, 2017

First I'd like to say that chapter 8 was excellent, from the perspective of this C++/C# developer, at least. I've just started learning Rust, so I've yet to read the other chapters. Mostly slacking off from the Rust book...

However, I was confused by the last example - it looks like traits can have functions implementations in them (the show method on the Window trait).
This surprised me for 2 reasons:

  • I had assumed that traits were kinda like Java/C# interfaces, that is, they can't contain implementations, while in fact now they look more like C++ "interfaces" in which essentially you can have both pure virtual methods but also implementations.
  • This looks to me like it enables implementation inheritance, which you derided several paragraphs above. Won't implementors of Window get show automatically?

You should perhaps clarify :

  • that it's possible and not an error to have an implementation in a trait
  • why this isn't actually enabling implementation inheritance.
@stevedonovan
Copy link
Owner

Yes, it does 'undermine' the message, so the message must be clarified. Traits are like modern Java interfaces, and may have default methods. (Case in point, Iterator - only have to define next(), get the rest for free). And this is a form of implementation inheritance, although not defined on the data. There's also a limited form of inheritance from Deref (String gets all the methods from &str for free).

I suppose I'm trying to shake the dogma tree here ;) Let me clarify this further! I suppose I wanted to hammer the point that there are no classes, just data + traits. But you can do similar tricks.

@stevedonovan
Copy link
Owner

Traits are like abstract C++ classes that don't have any data members, so additional virtual methods can be provided that only depend on other virtual methods. Except they can also be used as type constraints in generic functions. Allows for more flexible design trade-offs - don't have to always do polymorphic route.

@orvly
Copy link
Author

orvly commented May 16, 2017

Okay, it's much clearer now, thanks. I like the explanation in your comments. I think the key phrase is "this is a form of implementation inheritance, although not defined on the data." Adding this before or after the ShowWindows example would help, IMO.

Another thing: there's a broken link in the next to last paragraph, in the "Here is a rather promising minimal Windows GUI framework" sentence.

@stevedonovan
Copy link
Owner

Thanks for the comments, exactly the feedback I need! I was a bit down on traditional OOP, because the two forms of inheritance get confused. Java/C# at least separate out interfaces to avoid the tangled problem of multiple full inheritance. There's a simpler language inside Rust, but it would be more awkward. E.g. implementing Iterator would be a real bastard if there weren't provided methods. There's also Deref coercion where (for example) String inherits all the methods defined on &str. So let me mention these things up front, and not introduce any new concepts in the example. By the way, if you can think of any other OOP scenarios that could do with an example, I'm interested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants