Automatic Foreach
Bill Baxter
dnewsgroup at billbaxter.com
Sun Apr 27 01:24:21 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[]);
I think that already means call foo with a full slice of array, doesn't
it? Anyway, even if it doesn't you can overload opSlice() currently so
that array[] can mean anything you want it to.
--bb
More information about the Digitalmars-d
mailing list