View mode: basic / threaded / horizontal-split · Log in · Help
March 02, 2012
Extending UFCS to templates
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?
March 03, 2012
Re: Extending UFCS to templates
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.
March 03, 2012
Re: Extending UFCS to templates
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)
Top | Discussion index | About this forum | D home