Overlapping functionality: IFTI, templates, is-expressions

Russell Lewis webmaster at villagersonline.com
Wed Mar 19 14:56:41 PDT 2008


BCS wrote:
> Russell Lewis wrote:
>> As you can tell from my recent posts (and bug reports), I've been 
>> pounding on D's template mechanisms a lot recently.  I'm trying to 
>> build a parser generator using templates.  I'm now starting my 3rd 
>> major generation of the library, because I keep finding fundamental 
>> weaknesses in each method I try.
> 
> Do you /need/ one or do you want to /wright/ one? I ask because I have a 
> working version of this already.
> 
> http://www.dsource.org/projects/scrapple/browser/trunk/dparser

I don't know whether I *need* one...but it's a very fun exercise to 
write one.  I have your svn repository checked out on my machine...but I 
haven't delved deeping into it, yet.  From a cursory inspection, it 
didn't seem like it fit my needs.  But I may be wrong. :)

>  > [...]
> 
> I haven't read your proposals in detail but the first one seems 
> impractical because it would be very hard to construct the combined 
> template. Consider, for example, a template where it is used to build 
> other instances of it's self. Also I /think/ you can overload templates 
> from different modules if the args are different.

What I'm thinking is something like this:

ORIGINAL CODE (viewing template specialization as syntax sugar)

   template foo(T : int) { ... }
   template foo(T : char) { ... }

REWRITTEN CODE (expressing specialization as is-expressions)

   template foo(TPL...)
   {
     static if(TPL.length == 1 && is( TPL[0] == int )) { ... }
     else
     static if(TPL.length == 1 && is( TPL[0] == char)) { ... }
     else
     static assert(false, "ERROR: Unsupported specialization of foo");
   }

END CODE

> As to the second >1 tuple idea, firstly, it can be done in a round about 
> way (see below) and it has been proposed but no usable way to implement 
> it has been put forward. (A few ides have been floated but they have had 
> problems)

Certainly, we need (someday) a syntax to allow multiple tuples in the 
same set of parameters, and that will require some sort of "grouping" 
syntax for the instantiator.  And you can hack multiple-tuples using int 
params if you require the instantiators to play nicely with you (the 
first param is the length of the first tuple, the rest of the template 
parameters are one big tuple, the concatenation of your two tuples).

But, at least for some cases (specialization) there is some low-hanging 
fruit, such as:

* Args of function pointers & delegates

EXAMPLE CODE
   template foo(void delegate(ARGS...), REST...) { ... }
END CODE

* Typesafe tuples (analogous to typesafe variadic functions)

EXAMPLE CODE
   template foo(void delegate()... DGS, REST...) { ... }
END CODE

* Combine the two above, to give a tuple-of-tuples?

EXAMPLE CODE
   template foo(void delegate(ARGS...)... DGS, REST...) { ... }
END CODE



More information about the Digitalmars-d mailing list