In many cases, I want to write methods that have the same functionality for different types of inputs. This is easily accomplished by method overloading if the parameter types are different.
But what's the best (most robust) way to go about resolving the case when the parameter types are the same (i.e. two different representations of the data with the same type)?
An example of this would be an integer matrix which can naturally be stored as an int[][]
. But what if you want to write a method which accepts the transpose of the matrix as well? The transpose is also an int[][]
but a clearly different representation altogether.
I can see a couple ways of doing this:
- Giving the methods different names
- Adding a flag to the method
- Wrapping each representations in different classes
I think the third method is the most clear way of doing this. Unfortunately I'm working on some high performance libraries where that's not a feasible solution.