scoped classes and dependency inversion

Sjoerd Nijboer dlang at sjoerdnijboer.com
Thu Nov 8 11:04:19 UTC 2018


I'm trying to invert the dependency from the classes `Bar -> Foo` 
to `Foo -> IFoo <- Bar` at compile time.

I do want `Foo's` to be embedded into `Bar`

So silly me tried something like this:
module main;

```import std.stdio;
import std.typecons;

void main()
{
	auto bar = new Bar!(scoped!Foo());
}

class Bar (TFoo)
	if(is(TFoo == IFoo))
{
	scoped!TFoo _foo;
	this(scoped!TFoo foo)
	{
		_foo = foo;
	}
}

class Foo : IFoo
{
	void baz(){}
}

interface IFoo
{
	void baz();
}```

This fails to compile on line 8 since you can't move a scoped!Foo.
So how can I delay the construction of scoped!Foo that it'll end 
up inside Bar?
If there is a more elegant solution I'm also interested.


More information about the Digitalmars-d-learn mailing list