www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error: array operation d1[] + d2[] without assignment not implemented

reply "deed" <none none.none> writes:
struct Vector (T)
{
   T[]arr;
   void opSliceAssign (T[] a) { arr[] = a[]; }
}
unittest
{
   auto v = Vector!double([1, 2]);
   double[] d1 = [11, 12];
   double[] d2 = [21, 22];
   double[] d3 = new double[](2);
   d3[] = d1[] + d2[];
   assert (d3 == [11.+21., 12.+22.]);
   assert (is(typeof(d1[] + d2[]) == double[]));
   v[] = d1[]          // Fine
   v[] = d1[] + d2[];  // Error: array operation d1[] + d2[] 
without assignment not implemented
}

How can opSliceAssign be defined to make this work?
Sep 13 2014
next sibling parent reply "Ilya Yaroshenko" <ilyayaroshenko gmail.com> writes:
On Saturday, 13 September 2014 at 14:18:57 UTC, deed wrote:
 struct Vector (T)
 {
   T[]arr;
   void opSliceAssign (T[] a) { arr[] = a[]; }
 }
 unittest
 {
   auto v = Vector!double([1, 2]);
   double[] d1 = [11, 12];
   double[] d2 = [21, 22];
   double[] d3 = new double[](2);
   d3[] = d1[] + d2[];
   assert (d3 == [11.+21., 12.+22.]);
   assert (is(typeof(d1[] + d2[]) == double[]));
   v[] = d1[]          // Fine
   v[] = d1[] + d2[];  // Error: array operation d1[] + d2[] 
 without assignment not implemented
 }

 How can opSliceAssign be defined to make this work?
Hi! struct Vector (T) { T[]arr; T[] opSlice() { return arr; } } Vector!double v; double[] d; v[][] = d[] + d[]; //first [] call opSlise, second [] for array syntax Best Regards, Ilya
Sep 13 2014
parent "deed" <none none.none> writes:
 Hi!

 struct Vector (T)
 {
 	T[]arr;
 	T[] opSlice() { return arr; }
 }
 Vector!double v;
 double[] d;
 v[][] = d[] + d[];

 //first [] call opSlise, second [] for array syntax

 Best Regards,
 Ilya
Thanks for your suggestion. It's not as attractive though, it would be the same as v.arr[] = ..., exposing the naked array. The syntax also becomes a bit confusing. With alias this it works, but functionality is lost. See http://dpaste.dzfl.pl/35081c1f1745 It feels not consistent, so I guess that's the reason for the "not implemented" message.
Sep 13 2014
prev sibling parent "Ilya Yaroshenko" <ilyayaroshenko gmail.com> writes:
Operations like "ar1[] op= ar2[] op ar3[]" is only for arrays.
There is no operator overloading for this staff.
Sep 13 2014