Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Interface Template

Part of TemplatesCategory

Description

Shows how to use an interface template

Example

It wasn't mentioned in the docs, but I had a slight inkling that it might be the case that one could have InterfaceTemplates using the same syntax as ClassTemplates (ie class ClassName?(T) : ![superclass [, interfaces] ). The following example compiled and ran correctly, outputting "1\n4\n" as expected. Perhaps this warrants a mention in the documentation under Templates, alongside "Class Templates"?

interface Foo(T)
{
   int aFunction(T anArgument);
}

class Bar(T) : Foo!(T)
{
   int aFunction(T anArgument)
   {
      return anArgument.size;
   }
}

int main(char[][] args)
{
   Bar!(ubyte) one = new Bar!(ubyte);
   Bar!(int) two = new Bar!(int);

   ubyte oneVar = 16;
   int twoVar = 400000;

   printf("%i\n%i\n", one.aFunction(oneVar), two.aFunction(twoVar));

   return 0;
}

Source

From digitalmars.D:3828.