scoped classes and dependency inversion

Alex sascha.orlov at gmail.com
Thu Nov 8 12:45:57 UTC 2018


On Thursday, 8 November 2018 at 11:04:19 UTC, Sjoerd Nijboer 
wrote:
> 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:
> [...]
>
> 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.

Hmm... not sure, if I got your idea... Do you think about 
something like this?

´´´
import std.stdio;
import std.typecons;

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

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

class Foo : IFoo { void baz(){} }
interface IFoo{ void baz(); }
´´´




More information about the Digitalmars-d-learn mailing list