I've learned that a trait object is a type which has been coerced into a trait object plus it's vtable. But now I've found out that you can also add regular functions to trait objects eg via
impl dyn T, see here. Somehow this doesn't suit for me in the concept of a trait object in my mind. Does Rust add these functions to values of specific types which it coerces into trait objects or where are they lying around in memory?
Why would that be needed?
dyn Foo is concrete type, like
String. It works exactly like any other concrete type.
impl dyn Trait is the same as
impl AnyOtherType. When you already have a value of type
dyn Trait, the compiler will allow you to call these methods on it. This has nothing to do with the vtable or dynamic dispatch.
I see! Makes sense to me. Thanks!
... so eg here
b.demo(); gets dynamically dispatched and
b.demo2(); statically, right?
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.