Thread overview | |||||
---|---|---|---|---|---|
|
March 10, 2007 foreach in compile time function? | ||||
---|---|---|---|---|
| ||||
Is this code supposed to work? char[] test(char[] d) { char[] v=""; foreach(dchar c;d) v~=c; return v; } void main() { const a=test("test"); printf("%*.s",a); } I just get this error but I'm unsure if it is a bug or a limitation. johan@Cyclop:~/Desktop$ gdc -o test ctforeach.d ctforeach.d:5: Error: cannot evaluate _aApplycd1(d,__foreachbody1) at compile time ctforeach.d:11: Error: cannot evaluate test("test") at compile time johan@Cyclop:~/Desktop$ |
March 10, 2007 Re: foreach in compile time function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Granberg | Johan Granberg wrote:
> Is this code supposed to work?
>
> char[] test(char[] d)
> {
> char[] v="";
> foreach(dchar c;d)
> v~=c;
> return v;
> }
> void main()
> {
> const a=test("test");
> printf("%*.s",a);
> }
This is because you want foreach to implicitly convert your char array to dchars, and this happens in the compier runtime at, well, run-time. I imagine it would be possible to create compile-time routines to do this, but that feature isn't built into the compiler right now.
Sean
|
March 10, 2007 Re: foreach in compile time function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | Sean Kelly wrote:
> Johan Granberg wrote:
>> Is this code supposed to work?
>>
>> char[] test(char[] d)
>> {
>> char[] v="";
>> foreach(dchar c;d)
>> v~=c;
>> return v;
>> }
>> void main()
>> {
>> const a=test("test");
>> printf("%*.s",a);
>> }
>
> This is because you want foreach to implicitly convert your char array to dchars, and this happens in the compier runtime at, well, run-time. I imagine it would be possible to create compile-time routines to do this, but that feature isn't built into the compiler right now.
>
>
> Sean
Ok, it would be usefull if it worked thou.
|
Copyright © 1999-2021 by the D Language Foundation