top | item 47163395

(no title)

menaerus | 4 days ago

> only when needed

Do you know how is this exactly deduced?

discuss

order

dminik|4 days ago

It's not. The user has to decide.

A specific type/reference to a type will always use static dispatch.

fn foo(bar: &Baz) { bar.thing(); }

A dyn trait reference will always use dynamic dispatch and carry around the vtable pointer.

fn foo(bar: &dyn BazTrait) { bar.thing(); }

menaerus|4 days ago

Ah, I see. Do I understand correctly that this means that for a given instance of polymorphic object I can switch between static polymorphism and dynamic dispatch, and use them both simultaneously? How is this useful in practical terms, like why would I want to do it?