C++

Fredrik Olsson peylow at gmail.com
Fri Jul 7 11:53:01 PDT 2006


Walter Bright skrev:
> Chris Nicholson-Sauls wrote:
> 
>> Except for "Array operations not implemented" as yet.  Any idea when 
>> this much-coveted, much-anticipated feature will come to life?  I 
>> don't mean to push, but I've dreamt of array-ops nearly since I first 
>> got involved with D back in 2003.
> 
> 
> It'll have to be a 2.0 thing.
Perhaps not part of core language, put thought operator overloads. I 
guess there are a million reasons as to why not have them, but if I 
could choose this would be a nice syntax for both introducing operator 
overloads and properties to existing types:

type int[] {

     int[] opAdd(int[] a) {
         assert(this.length == a.length);
         int[] r; r.length = this.length;
         foreach (i, v; this) {
             r[i] = v + a[i];
         }
         return r;
     }

     int sum() {
         int r = 0;
         foreach(v; this) {
             r += v;
         }
         return v;
     }

}

// Making these work;
int[] a = [1, 2, 3];
int[] b = [4, 5, 6];
int[] c = a + b; // [5, 7, 9];
int d = c.sum(); // 15


// Fredrik



More information about the Digitalmars-d mailing list