I'm trying to make a SparseSet that on function of a optional type parameter, could alongside the index set, store other data. So I need a way to declare a optional type template parameter.

I prototyped this stuff on run.dlang, but I like know if there is a better way :

https://run.dlang.io/is/Uhy5IT

import std;

struct S {
        int x = 0;

    string toString()
    {
        return "S(x="~ x.to!string ~")";
    }
}

struct ZeroOrMore(T = uint, Types...)
    if (__traits(isUnsigned, T))
{
    static assert (Types.length == 0 || Types.length == 1);

    T[] _t;

    static if (Types.length > 0) {
        alias V = Types[0];
        V[] _values;

        void ins(T t, V val)
        {
            this._t ~= t;
            this._values ~= val;
        }
    } else {
        void ins(T t)
        {
            this._t ~= t;
        }
    }
}

void main()
{
    auto s = ZeroOrMore!()();
// trying to use ZeroOrMore() gives error : struct onlineapp.ZeroOrMore cannot deduce function from argument types !()(), candidates are: onlineapp.d(12): ZeroOrMore(T = uint, Types...)
    s.ins(456);

    auto s2 = ZeroOrMore!(uint, S)();
    s2.ins(123, S(666));

    writeln(s); // ZeroOrMore!uint([456])
    writeln(s2); // ZeroOrMore!(uint, S)([123], [S(x=666)])
}

Reply via email to