Automatic Foreach

janderson askme at me.com
Sun Apr 27 01:23:04 PDT 2008


janderson wrote:
> Bill Baxter wrote:
>> janderson wrote:
>>> What do you think?
>>
>> I think it's already hard enough to figure out what's going to get 
>> called by something like foo(array)
>>
>> It could be
>> - foo(int[] x)
>> - foo(int[] x...)
>> - foo(T)(T x)
>> - foo(T)(T[] x)
>> - foo(T...)(T x)
>> - struct foo { static foo opCall(int[]); }
>> - struct foo { static foo opCall(int[]...); }
>> - struct foo { static foo opCall(T)(T x); }  etc
>> - or struct Foo with non-static opCall on instance foo
>> - or all the same stuff on class  ...
>>
>> So I don't think another meaning for foo(array) is really helpful.
>>
>> I *do* like the idea of an expression (not a statement) that has 
>> foreach-like abilities.  But I think it should come with some 
>> distinguishing syntax.
>>
>> In Python it's just [expr(x) for x in array]
>> Which resonates with Pythons normal loopoing:
>>    for x in array: expr(x)
>>
>> So direct translation of that idea to D would be
>>    [expr(x) foreach(x; array)];
>>
>> Seems not so terrible a syntax to me.
>>
>> --bb
> 
> I think I prefer:
> 
> foo(array[]);
> 
> -Joel


That could work with member functions too:

array[].foo();

-Joel



More information about the Digitalmars-d mailing list