Template Interface
Nathan M. Swan
nathanmswan at gmail.com
Tue Jun 12 10:56:25 PDT 2012
When writing a generic function which takes an unknown type, the
signature is written like so:
void fun(L)(L l) if (isList!L);
While writing a generic interface is written like so:
template isList(L) {
enum bool isList = is(typeof(
(inout int _dummy=0)
{
L l;
if (l.nil) {}
auto e = l.car;
l = l.cdr;
));
}
This doesn't seem very intuitive to me. OOP languages handle this
with interfaces, but in D for things like ranges we often use
structs, making it incompatible with the current "interface."
I'm suggesting something like a "template interface", which is
compatible with all types, and serves as a placeholder for any
type for which the body compiles:
void fun(List l);
template interface List {
List l;
if (l.nil) {}
auto e = l.car;
l = l.cdr;
}
It might be have parameters:
void fun(List!string l);
template interface List(E) : List {
List!E l;
E e = l.car;
}
It makes writing generic code much cleaner.
Thoughts?
NMS
More information about the Digitalmars-d
mailing list