www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Extending UFCS to templates

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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?
Mar 02 2012
next sibling parent "Kapps" <opantm2+spam gmail.com> writes:
On Friday, 2 March 2012 at 21:18:31 UTC, Andrej Mitrovic 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?

From what I understand, UFCS should work on templates once properly implemented. A.someNonExistentThing(params) basically becomes .someNonExistentThing(A, params) I think that (at least in the current UFCS pull request) it *should* work, but I've never actually tried it.
Mar 02 2012
prev sibling parent kennytm <kennytm gmail.com> writes:
Andrej Mitrovic <andrej.mitrovich 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)
Mar 03 2012