Extending UFCS to templates

kennytm kennytm at gmail.com
Sat Mar 3 14:08:23 PST 2012


Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
> I thought I'd be cool if we could use UFCS for templates. For example
> instead of this:
> 
> template isOneOf(X, T...)
> {
>     static if (!T.length)
>         enum bool isOneOf = false;
>     else static if (is(Unqual!X == T[0]))
>         enum bool isOneOf = true;
>     else
>         enum bool isOneOf = isOneOf!(Unqual!X, T[1..$]);
> }
> 
> void test(T)(T t)
>     if (isOneOf!(T, int, double))
> { }
> 
> void main() {
>     test(1);
> }
> 
> We would be able use code like this:
> 
> void test(T)(T t)
>     if (T.isOneOf!(int, double))  // UFCS (or maybe Uniform Template
> Instantiation Syntax? :p)
> { }
> 
> A far-fetched dream?

This only works for types. Values (thus, aliases) would be ambiguous. 

    1.isOneOf!(2, 3)
    // could be isOneOf!(1, 2, 3)
    // or isOneOf!(2, 3)(1)


More information about the Digitalmars-d mailing list