Dyn dispatch on multiple types

The code in the playground is the essential building block for multiple dispatch; you can trivially swap out the literal println!()s for trait methods implementing the classic visitor pattern as discussed above (i.e., simply by using separate functions instead of a "single"/overloaded method), like this. Here, both parameters of both visitor methods are statically typed, there is no dyn Trait involved at this level.

To better demonstrate that claim of mine, here is another version that contains functions with all argument types spelled out explicitly (which the trait methods forward to), as well as a single generic double_dispatch() function that hides the dyn Trait method calls and appears as a regular free function with two arguments.

3 Likes