Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function overloading in Dart

The following code:

class Tools {   static int roll(int min, int max) {     // IMPLEMENTATION   }    static int roll(List<int> pair) {     // IMPLEMENTATION   } } 

renders a The name 'roll' is already defined error on the second roll function.

How come? Since the parameters of the functions are distinct, shouldn't polymorphism apply?

Edit. Corrected title in order to better reflect the subject.

like image 524
Calebe Avatar asked Apr 09 '18 13:04

Calebe


People also ask

What is function overloading?

An overloaded function is really just a set of different functions that happen to have the same name. The determination of which function to use for a particular call is resolved at compile time. In Java, function overloading is also known as compile-time polymorphism and static polymorphism.

What is operator overloading in Dart?

Fortunately, Dart allows this functionality with operator overloading, which means that you can take the standard operators and let your own classes provide meaning to them. In this instance, you want to provide meaning to the greater-than and less-than operator in the context of the Role class.

What is method overloading example?

In Java, two or more methods may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is called method overloading. For example: void func() { ... }

What is function overloading in OOP with example?

Function overloading is a C++ programming feature that allows us to have more than one function having same name but different parameter list, when I say parameter list, it means the data type and sequence of the parameters, for example the parameters list of a function myfuncn(int a, float b) is (int, float) which is ...


2 Answers

What your code demonstrates is function overloading and not related to polymorphism.

Function overloading is not supported in Dart at all.

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 toghether (only one or the other for a single function)

Polymorphism and static methods:

Static methods can only be accessed without the class name as prefix from inside the class where they are defined. When called from subclasses, the name of the superclass needs to be used as prefix.

like image 147
Günter Zöchbauer Avatar answered Oct 05 '22 16:10

Günter Zöchbauer


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<int> pair; }  // this is the function int roll<T extends RollArguments> (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.

like image 23
Santiago Hernández Avatar answered Oct 05 '22 17:10

Santiago Hernández