Relm4 / book

The Relm4 book
https://relm4.org/book/stable
MIT License
13 stars 22 forks source link

I do not really understand this sentence #30

Closed tronta closed 1 year ago

tronta commented 1 year ago

May be somebody can help me understanding this sentence better: https://github.com/Relm4/book/blob/9a9dced99f8ff7ce9251b4ecb55aa1ebfbe24171/src/basic_concepts/basics/components.md?plain=1#L11

If there is already an implementation for every type, why do I need to implement one?

AaronErhardt commented 1 year ago

The sentence is basically a summary of blanket implementations. If your type implements SimpleComponent, a generic Component implementation will also implement Component for this type. Internationally, only Component is used, which works for both trait thanks to the blanket implementation. This is similar to From which has a blanket implementation for Into. Also, implementing both manually will throw an error.

Maybe you have an idea to make this easier to understand?

tronta commented 1 year ago

Thanks for sharing. Again something new learned.

My try:

As SimpleCompoment is automatically converted into a Component you can decide which one you want to implement.

AaronErhardt commented 1 year ago

It's not actually converted though. It's more like inheritance in other languages: SimpleComponent would be a sub-class of Component in C++ for example. SimpleComponent is still its own trait, but it automatically implements Component for you, so you can treat it as if your type was a regular Component.

tronta commented 1 year ago

take two: When using SimpleComponent a default implementation for the missing functions is automatically created, so you can use SimpleComponent and Component interchangeable.

AaronErhardt commented 1 year ago

Also not 100% correct. SimpleComponent not just implements the missing methods, it is it's own standalone trait. Also, default implementations are actually used in another context so this will only lead to more confusion, I assume.

Maybe we can put it like this: For every implementation of SimpleComponent Relm4 will also implement Component automatically. You just need to implement one of them and your type will implement Component either way. This is called blanket implementation and is used for traits like From in the standard library too [insert useful link here].

tronta commented 1 year ago

For me that would be a big improvement.

AaronErhardt commented 1 year ago

Awesome, it's always good to address some unclear sentences in the book. Do you want to send a PR?

tronta commented 1 year ago

Sure, I'll do. Thanks.

tronta commented 1 year ago

Would this be a useful link? https://doc.rust-lang.org/book/ch10-02-traits.html

AaronErhardt commented 1 year ago

I think this would be more precise: https://doc.rust-lang.org/book/ch10-02-traits.html#using-trait-bounds-to-conditionally-implement-methods

tronta commented 1 year ago

https://github.com/Relm4/book/pull/31