Call a function on an entire slice

monarch_dodra monarchdodra at gmail.com
Wed Aug 29 06:38:30 PDT 2012


D provides ways to operate on an entire (sub) slice:
int[] a = ... ;
a[] *= 5; //Multiply everything by 5.
a[0 .. 2] = 3; //Set indexes 0 to 1 to the value 3.

I was wondering if there was any way to do this for a specified 
function?

----
struct S
{
     void foo()
     {
         writeln("foo");
     }
}

void bar(S s)
{
     writeln("bar");
}

void main()
{
   S[5] a;
   a[].foo(); //undefined identifier 'foo'
   a[].bar(); //cannot implicitly convert expression (a[]) of type 
S[] to S
};
----
I know I can just foreach it, but it doesn't quite have the same 
expressive power.

If the built in arithmetic types have this power, why not 
functions?


More information about the Digitalmars-d-learn mailing list