DIP10005: Dependency-Carrying Declarations is now available for community feedback

ArturG via Digitalmars-d digitalmars-d at puremagic.com
Fri Dec 30 15:59:43 PST 2016


On Friday, 30 December 2016 at 23:49:23 UTC, Andrei Alexandrescu 
wrote:

> The main win, which indeed is not emphasized enough, is better 
> encapsulation. Walter pointed that out, and I will redo the DIP 
> to put that front and center.

Maybe i can provide an example where i think DCD's would be 
usefull.
Im experimenting with a more Qt like signals and slots 
implementation,
it introspects some type for function declarations with the 
@("Signal") uda
and mixes them into the current type.
It currently looks like this:

// moda.d
interface TestSignals
{
    import modb: TestType;
    @("Signal"):
       void someTestSig(TestType);
}

// modb.d
struct TestType{}

// test.d
class A : SignalObject
{
    import moda: TestSignals;
    import modb: TestType; // has to be manually imported for the 
generated signal

    mixin signalsOf!(SignalList, TestSignals);

    interface SignalList
    {
       @("Signal"):
          void foo(int);
          void bar(string, int = 100);
          int bar(int a, int b);
    }

    void someFun(string s) { s.writeln; }
}

class B : SignalObject
{
    struct SignalList
    {
       @("Signal"):
          void someSig(string);
    }

    mixin signalsOf!SignalList;

    void fooHandler(int i){ i.writeln; }

    int onBar(int a, int b){ writeln(a + b); return a + b; }
    void onBar(string s, int i){ writeln(s, i); }

    import modb: TestType;
    void someTestSigHandler(TestType t){ t.writeln; }
}

class C : SignalObject
{
    import modb: TestType; // has to be manually imported for the 
generated signal

    mixin signalsOf!(A, B);

    // i wanted to support string based signal declaration
    // but that would require double manual import declarations

    // import modb: TestType;

    // enum signalList =
    // q{
    //    import modb: TestType;
    //    @("Signal"):
    //    void someSig(TestType);
    //    int someOtherSig();
    // }

    // mixin signalsOf!signalList;
}

void main(string[] args)
{
    auto a = new A;
    auto b = new B;
    auto c = new C;

    a.connect!"foo"(&b.fooHandler);
    a.connect(b); // connect all bar's with all onBar's

    a.foo(66);
    a.bar("asd");
    a.bar(4, 6);

    c.connect!"someSig"(&b.someSig);
    b.connect!"someSig"(&a.someFun);

    c.someSig("emitted by c forwarded by b handled by a");

    a.connect!"someTestSig"(&b.someTestSigHandler);
    c.connect!"someTestSig"(&b.someTestSigHandler);

    import modb: TestType;
    TestType t;
    a.someTestSig(t);
    c.someTestSig(t);
}

i assume with DCD's i could remove those manual imports if they 
get some traits.




More information about the Digitalmars-d mailing list