Template Interface

Mehrdad wfunction at hotmail.com
Wed Jun 13 13:11:15 PDT 2012


On Tuesday, 12 June 2012 at 17:56:26 UTC, Nathan M. Swan wrote:
> 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:

What you're looking for were also proposed for C++; they were 
called "concepts"

> 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