Search⌘ K

Function Overloading and Overload Resolution

Explore the concept of function overloading in D, where multiple functions share the same name but differ in parameters. Understand how the compiler resolves which function to call by evaluating match quality among overloads. This lesson helps you grasp overload resolution rules and how to manage ambiguities effectively.

We'll cover the following...

Function overloading

Defining more than one function that have the same name is function overloading. In order to be able to differentiate these functions, their parameters must be different.

The following code has multiple overloads of the info() function, each taking a different type of parameter:

D
import std.stdio;
void info(double number) {
writeln("Floating point: ", number);
}
void info(int number) {
writeln("Integer : ", number);
}
void info(string str) {
writeln("String : ", str);
}
void main() {
info(1.2);
info(3);
info("hello");
}

Although all of the functions are named info(), the compiler picks the one that matches the argument that is used when making the call. For example, because the literal 1.2 is of type double, the info() function that takes a ...

svg viewer