Call proper function based on parameter's dynamic type

To add to Lantua's answer, there are three ways to get type-based dynamic dispatch behavior in Swift:

  • Call a protocol requirement
  • Call a (non-final) method on a class
  • Manually check types with as?

It can get confusing sometimes because the compiler will pick the best overload based on the static type, which can look like dynamic dispatch. But this list is the only real way.

7 Likes