Question of delegate literals and return from function(GDC, DMD, LDC)
Daniel Keep
daniel.keep.lists at gmail.com
Sun Feb 1 13:46:47 PST 2009
downs wrote:
> amaury pouly wrote:
>> Hello,
>> I have a little question about D: I want to have a function returning a delegate and more precisely a delegate literal that uses one of the arguments of the function. Here is an example:
>>
>> int delegate(int) test(int[] tab)
>> {
>> return (int d)
>> {
>> int sum;
>> foreach(i;tab)
>> sum+=i*d;// stupid computation
>> return sum;
>> };
>> }
>>
>> int main()
>> {
>> auto res=test([1,2,3,4,5,6])(4);
>> return res;
>> }
>
> tools version:
>
> import tools.base, std.stdio;
>
> int delegate(int) test(int[] tab) {
> return tab /apply/ (int[] tab, int d) {
> int sum;
> foreach (i; tab) sum += i*d;
> return sum;
> };
> }
>
> void main() {
> auto res = test([1,2,3,4,5,6])(4);
> writefln(res);
> }
I was tempted to post mine out of sheer bloody-mindedness, except that
it apparently doesn't work any more.
Oh well, yours was about a million times safer, anyway. :P
-- Daniel
More information about the Digitalmars-d
mailing list