[Article Submission] Have Your Efficiency, and Flexibility Too

Timon Gehr timon.gehr at gmx.ch
Wed Jun 1 01:33:52 PDT 2011


Nick Sabalausky wrote:
> "bearophile" <bearophileHUGS at lycos.com> wrote in message
> news:is45u7$21vq$1 at digitalmars.com...
>> Nick Sabalausky:
>>
>>> Error: template instance cannot use local 'np' as parameter to non-global
>>> template addGizmosTo(int numPorts,bool isSpinnable)
>>
>> Are you able to reduce this to a minimal test case?
>>
>
> If it is indeed a bug then I'll try. But the fact that there's an error
> message for it seems to indicate this is intended for some reason. After
> all, addGizmosTo *is* a non-global template, just like the message says. And
> 'np' shouldn't be visible outside the function it's in, ie, it's local.
>

'np' is just an integer literal at the point of instantiation. Would you want to
not be able to call a function with local variables as parameters either?

Eg:
int main(){
    int foo(int){...}
    foreach(np;[1,2,3,4,5]) foo(np);
}

If the bug was there for runtime foreach too, this would print

Error: function call cannot use local 'np' as parameter to non-global function foo.

This would also be a fit for

It is certainly a bug, there is no reason why it should not work. (an integer
literal is not bound to a scope).

Minimal test case:

import std.typetuple;
void main(){
    int foo(int x)(){return x;}
    foreach(i;TypeTuple!(0)) foo!(i);
}

Bug report:
http://d.puremagic.com/issues/show_bug.cgi?id=6084

Probably, the Error is intended for the following case:

int bar(int x)(){return x;}

void main(){
    immutable int i=0;
    int foo(int x)(){return x;}
    foo!i; //error
    bar!i; //ok
}

Is there any reason why this should not work? Is it just an implementation issue?
(I have heard of immutable compile-time values not yet working properly.)

Workaround for the bug:

import std.typetuple;
void main(){
    int foo(int x)(){return x;}
    foreach(_i;TypeTuple!(0)){
        enum i=_i;
	foo!i;
    }
}

I think it should be possible to fix it for TypeTuple foreach by having the
iteration variable be enum. I'll have a look at the compiler.


Timon


More information about the Digitalmars-d mailing list