Closed tronta closed 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?
Thanks for sharing. Again something new learned.
My try:
As
SimpleCompoment
is automatically converted into aComponent
you can decide which one you want to implement.
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.
take two:
When using SimpleComponent
a default implementation for the missing functions is automatically created, so you can use SimpleComponent
and Component
interchangeable.
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].
For me that would be a big improvement.
Awesome, it's always good to address some unclear sentences in the book. Do you want to send a PR?
Sure, I'll do. Thanks.
Would this be a useful link? https://doc.rust-lang.org/book/ch10-02-traits.html
I think this would be more precise: https://doc.rust-lang.org/book/ch10-02-traits.html#using-trait-bounds-to-conditionally-implement-methods
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?