Call a function on an entire slice
    Timon Gehr 
    timon.gehr at gmx.ch
       
    Wed Aug 29 07:06:53 PDT 2012
    
    
  
On 08/29/2012 03:38 PM, monarch_dodra wrote:
> 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.
>
Yes it does.
> If the built in arithmetic types have this power, why not functions?
Because the built-in arithmetic vector operators are supported by hardware.
    
    
More information about the Digitalmars-d-learn
mailing list