On Thursday, 8 November 2018 at 15:11:16 UTC, Sjoerd Nijboer wrote:
Except if you want to pass a parameter to TFoo it'll become a mess.
And I expecially don't want it to become messy.

I thought of this case... But passing the argument to TFoo directly while constructing Bar is messier, I think. With the solution above you could just:

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

void main()
{
        auto initData = 42;
        auto bar = new Bar!Foo(initData);
        assert(bar._foo.orWhatTypeEver == initData);
}

class Bar(TFoo) if(is(TFoo : IFoo))
{
        typeof(scoped!TFoo()) _foo;
        this(T)(T fooParam) //if(__traits(compiles, new TFoo(T.init)))
        {
                _foo = scoped!TFoo(fooParam);
        }
}

class Foo : IFoo
{
        int orWhatTypeEver;
        this(T)(T myParam)
        {
                orWhatTypeEver = /*cast(int)*/myParam;
        }
        void baz(){}
}
interface IFoo{ void baz(); }
´´´

In this case, Bar's constructor is not bound to any special type of input, letting Foo handle any input you want. Still, the solution is type safe.

And, maybe to calm my mind: Bar is the context of Foo, so it is allowed to see its inputs...

Reply via email to