www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - A request for an easier way to pass a storage class to a template

struct Signal(Types...)
{
    alias void delegate(Types) DG;

    void add(DG dg)
    {
        dgs ~= dg;
    }

    DG[] dgs;
}

struct Workaround(alias DG)
{
    void add(typeof(DG) dg)
    {
        dgs ~= dg;
    }

    typeof(DG)[] dgs;
}

void main()
{
    Signal!(int) foo;      // ok
    foo.add( (int){} );

    Signal!(ref int) bar;   // NG
    bar.add( (ref int){} );

    Workaround!((ref int){}) doo;  // workaround
    doo.add( (ref int){} );
}

Currently you can't create signals with ref parameters (std.signals),
nor the new one by Johannes Pfau (you can but it's problematic due to
nonref<>ref handler bugs, described in another d.learn thread).

I did find a workaround, but it's not very pretty. You have to create
delegates just to pass a storage class around.

Is there anything planned or been talked about w.r.t. passing storage
classes along with type parameters?

I know about ParameterStorageClassTuple and friends, but that's hardly
useful in this case. You would still need to define a function, which
means even more visual bloat! :)
Oct 24 2011