Proposal: static template(fail)
BCS
ao at pathlink.com
Fri Dec 14 12:04:14 PST 2007
Reply to Janice,
> On 12/14/07, BCS <ao at pathlink.com> wrote:
>
>> Example (each case maintained independently)
>>
> I don't know if this is reasonable, because you've got multiple
> different versions of Foo(T). But maybe...
>
>> template Foo(T)
>> {
>> static if(/*logic 1 on T*/)
>> /* code */
>> else
>> template(fail);
>> }
>> template Foo(T)
>> {
>> static if(/*logic 2 on T*/)
>> /* code */
>> else
>> template(fail);
>> }
>> template Foo(T)
>> {
>> /* general case code */
>> }
> My feeling is, you could recode that as
>
> template Foo(T)
> {
> static if(/*logic 1 on T*/)
> /* code */
> else static if(/*logic 2 on T*/)
> /* code */
> else
> /* general case code */
> }
> without needing either specialisation or template(fail). (But there
> are other cases where it /would/ be useful, obviously).
>
That only works if the maintainers of the two templates work together. Here
is an example where they wouldn't be:
//////
module A;
void Func(T)(T t); // does some stuff with a T, Expects to find a Go(T)
that works for T
//////
module B;
import A;
void Go(T: B.Type)(T t); // library that works with B.Type and interacts
with A.Func
//////
module C;
import A;
void Go(T: C.Thing)(T t); // different library that works with C.Thing and
interacts with A.Func
//////
module D;
import A;
import B;
import C;
// use it all here
Now that works as is (assuming no errors in my typing) but if you now want
to have a module that defines a Go(T) that uses static if's to find if the
T is something it can handle, you can only have one of those.
More information about the Digitalmars-d
mailing list