1
votes

Consider a function foo that takes another function f and data of unknown type d. If neither the return type of f nor the type of d are known at the time of function definition, how might the function declaration be specified? That is, how can I express something like the following:

void foo(_ f, _ d) {
    // ..

where _ indicates a wildcard/catch-all match for the type.

Another question by extension: Is it possible to specifying that the types be the same if their identity can be anything? In other words _ might be any type, but the types need to be the same.

1
Upon closer review, it seems like templates (dlang.org/spec/template.html) are designed to help with these problems by providing a way to make functions generic. If any cares to suggest other approaches, please share. - David Shaked

1 Answers

2
votes

Functions have to know their type. It would be pretty hard to generate code for them otherwise, and the linker certainly couldn't deal with them. That being said, like C++, D has templates, so you can declare function templates so that the function itself is generated and compiled when code using it is compiled. So, if you want a function that takes two arguments of the same type but will work with various types, then you'll want to use a function template. e.g.

void foo(T)(T a, T b)
{
    ...
}

Or if multiple types were needed, you could do something like

void foo(T, U)(T a, U b)
{
    ...
}

In either case, when code then calls foo, the types of the parameters will be inferred by the compiler from the types of the actual arguments. Calling the same function template with different argument types will result in additional functions being generated by the compiler. The relevant part of the official docs is here:

http://dlang.org/spec/template.html#function-templates

But this chapter from an online book would probably be more informative:

http://ddili.org/ders/d.en/templates.html

I'd suggest that you consider reading the whole book (or at least looking it over) if you want something that explains a lot of the basics of D.