Search⌘ K
AI Features

Function Overloading for User-Defined Types

Explore how to use function overloading for user-defined types such as structs and classes in D. Understand how to create specific overloads for methods like info() to properly handle custom types and manage output streams effectively.

Function overloading for user-defined types

Function overloading is useful with structs and classes. Furthermore, overload resolution ambiguities are much less frequent with user-defined types. Let’s overload the info() function above for some of the types that we have defined in the structs chapter:

struct TimeOfDay {
    int hour;
    int minute; 
}
void info(TimeOfDay time) {
    writef("%02s:%02s", time.hour, time.minute); 
}

This overload enables TimeOfDay objects to be used with ...