The following code:
class Tools {
static int roll(int min, int max) {
// IMPLEMENTATION
}
static int roll(List pair) {
// IMPLEMENTATIO
All right, this is quite old. But this is an approach that could help someone in the future. Relies a lot in generics, so in order to perceive the beauty you should be very familiar with that concept.
This is a very useless and absurd example:
// these are the overloads
class RollArguments { }
class FromMinAndMax extends RollArguments {
int min;
int max;
}
class FromList extends RollArguments {
List pair;
}
// this is the function
int roll (T r) {
var min = 0;
var max = 0;
if (r is FromMinAndMax) {
min = r.min;
max = r.max;
}
else if (r is FromList) {
min = r.pair[0];
max = r.pair[1];
}
print("min = $min; max = $max");
return 1;
}
usage of the function would be something like this:
roll(FromMinAndMax()
..min = 0
..max = 100
);
roll(FromList()
..pair = [0, 200]
);
The major downside of this approach is that you can not control whether the parameters are optional or not.
Maybe you could think that the overload name of the functions are in the derived types, and it is too verbose. But you could reuse a bit of code if you implement it correctly and improve readability in some cases too.
Anyway, this is just another way to achieve function overloading, there are many more ways for different cases.