Function with a single method acting on Union{T1, T2} or another function with two methods, for T1 and T2?

This is similar to

Generically, the area2 approach has two benefits:

  • it can be expanded to additional shapes
  • it guarantees compile time dispatch (if the rest of the code is type stable)
    Perhaps the logical approach would be to set up Square and Rectangle as subtypes of Quadrilateral. But that depends on what else you are doing with those types.
3 Likes