Equivalents to policy classes in D

Henry Robbins Gouk henry.gouk at gmail.com
Mon Apr 2 19:54:08 PDT 2012


I'm not all that familiar with policy classes but, if I 
understand what you are asking correctly, you can provide 
implementations in interfaces if the methods are final or static.

Is this the sort of thing you mean?

import std.stdio;

interface Foo
{
	final string foo()
	{
		return "foo";
	}
}

interface Bar
{
	final string bar()
	{
		return "bar";
	}
}

template IsInterface(T)
{
	enum IsInterface = is(T == interface);
}

class Policy(T, U) if(IsInterface!T && IsInterface!U) : T, U
{

}

alias Policy!(Foo, Bar) FooBar;

void main()
{
	auto fb = new FooBar();
	writeln(fb.foo(), " and ", fb.bar());
}


More information about the Digitalmars-d-learn mailing list