Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does dart not allow method overloading?

Tags:

dart

I tried to use method overloading in some dart code and quickly learned that overloading is not offered in dart.

My questions are: why is it not offered, and what is the recommended alternative? Is there a standard naming convention since methods that do the same thing but with different inputs must have different names?

Is it standard to use named parameters and then check that the caller has supplied enough information to complete the calculation?

Say I have a method that returns how much money someone makes in a year, called yearlyIncome.

In Java, I would create a method like this

double yearlyIncome(double hourlyRate, double hoursWorkedPerYear)

And maybe another method like this

double yearlyIncome(double monthlyRate, int monthsWorkedPerYear)

and so on. They're all used to calculate the same thing, but with different inputs. What's the best, standardized way to do this in dart?

Thanks so much in advance.

like image 920
loganrussell48 Avatar asked Jun 28 '20 18:06

loganrussell48


2 Answers

Function overloading is not supported in Dart at all. Function overloading requires static types. Dart at its core is a dynamically typed language.

You can either use different names for the methods or optional named or unnamed parameters

// optional unnamed
void foo(int a, [String b]);

foo(5);
foo(5, 'bar');

// optional named
void foo(int a, {String b});

foo(5);
foo(5, b :'bar');

Optional parameters can also have default values. Optional named and unnamed parameters can not be used together (only one or the other for a single function) In the case of a constructor you can use named constructors as an alternative

like image 128
barbecu Avatar answered Oct 21 '22 04:10

barbecu


Dart did not support overloading originally because it was a much more dynamic language where the declared types did not have any semantic effect. That made it impossible to use static type based overload resolution.

Dart has since changed to be more statically type, and there is nothing fundamentally preventing Dart from adding overloading today, except that it would be a huge work and a huge change to the language. Or so I'd assume, because there isn't any obvious design that isn't either highly complicated or hugely breaking.

What you do instead in Dart is to use optional parameters. A method like:

String toString([int radix]);

effectively have two signatures: String Function() and String Function(int). It can act at both signatures. There are definite limits to how far you can go with just optional parameters, because they still need to have exactly one type each, but that is the alternative that Dart currently provides. (Or use different names, but that's not overloading, you can do that in languages with overloading too).

Optional parameters is also one of the complications if we wanted to add overloading to the Dart language - would existing functions with optional parameters would count as multiple overloadings? If you declare a class like:

abstract class WithOverloading {
  String toString();
  String toString(int radix);
}

is that then the same signature as:

abstract class WithoutOverloading {
  String toString([int radix]);
}

Probably not because you can tear off the latter and get one function with an optional parameter, and you might not be able to tear off both functions from the former and combine them into one function. Or maybe you can, that's why it's not a trivial design question how to include overloading into the existing Dart language.

like image 41
lrn Avatar answered Oct 21 '22 05:10

lrn