Implicit Interface Deduction

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 16 11:22:23 PST 2015


On Sunday, 13 December 2015 at 22:09:47 UTC, Faux Amis wrote:
> interface IA {}
> interface IB {}
> interface IC {}
> interface IAB : IA, IB {}
> interface IBC : IB, IC {}
>
> class C : IA, IB, IC {}
> // Defining C as : IAB, IBC
> // is not really scalable ;)
>
> void main()
> {
>  IAB c = new C(); // This doesn't work.
> }
> // Any suggestions?

I believe you can use std.typecons.wrap for this.

import std.typecons: wrap;
import std.stdio;

interface IA
{
	void quack();
}

interface IB
{
	int divBy2(int n);
}

interface IAB: IA, IB
{
}

class Test: IA, IB
{
	override void quack()
	{
		writeln("quack!");
	}

	override int divBy2(int n)
	{
		return n / 2;
	}
}

void main()
{
	auto t = new Test();
	IAB tAsIAB = t.wrap!IAB;
	tAsIAB.quack();
	writeln(tAsIAB.divBy2(4));
	assert(!is(Test : IAB));
	assert(is(tAsIAB: IAB));
}


More information about the Digitalmars-d-learn mailing list