Automatic Foreach

janderson askme at me.com
Tue Apr 29 22:09:57 PDT 2008


Bruno Medeiros wrote:
> janderson wrote:
>> while back I wrote a template that works like:
>>
>> void foo(int i)
>> {
>>   printf("B");
>> }
>>
>> alias AutoForeach!(foo, int) foo;
>>
>> Then I could go:
>>
>> for(6); //Prints "B"
>> int[] array;
>> array = 2;
>> foo(array); //Prints "BB"
>>
>> But then I couldn't specialize it later without removing the alias, 
>> because I'd get a name clash.
>>
>> The other problem is this won't do cases like:
>>
>> result ~= foo(array);
>>
> 
> Why not? Didn't you define AutoForeach!(foo, int) to return an int[] ?

Your right a AutoForEachReturn could work here.

> 
>> or
>>
>> foo(foo1(array));  //Runs foo and foo1 array.length times.
>>
>>

I forgot to mention that in that example foo1 was defined with the 
autoforeach, foo was not (and it wouldn't work even if I did).

> 
> Same as above.
> 



More information about the Digitalmars-d mailing list