array operation a[] + b[] not implemented??

dsimcha dsimcha at yahoo.com
Sun Jan 17 16:49:39 PST 2010


== Quote from Matti Niemenmaa (see_signature at for.real.address)'s article
> On 2010-01-18 00:42, Trass3r wrote:
> > It is implemented in the runtime so why doesn't it work?
> >
> >
> > /***********************
> > * Computes:
> > * a[] = b[] + c[]
> > */
> >
> > T[] _arraySliceSliceAddSliceAssign_f(T[] a, T[] c, T[] b)
> > ...
> > ...
> >
> >
> >
> > void main()
> > {
> > float[] a = [0.f, 0.5f, 1.0f, 1.5f, 2.0f, 2.5f];
> > float[] b = [0.f, 0.5f, 1.0f, 1.5f, 2.0f, 2.5f];
> >
> > // float[] c = a[] + b[]; // <-- Array operation a[] + b[] not implemented
> > float[] d = a[] * 4.f + 1.f;
> > writeln(d); // <-- access violation when program is run
> > }
> This is Bug 3066: http://d.puremagic.com/issues/show_bug.cgi?id=3066
> Your code is invalid, since per the spec
> (http://www.digitalmars.com/d/1.0/arrays.html) array operations are
> valid only on the RHS of an assignment when there's a slice on the LHS.
> In your case, you're not performing an assignment at all, you're
> initializing, and array operations are not valid initializers.
> To fix, preallocate and then perform the array op:
> auto c = new float[a.length];
> c[] = a[] + b[];
> auto d = new float[a.length];
> d[] = a[] * 4.f + 1.f;

As a side note, why isn't this allowed?  It seems like a perfectly reasonable
thing to have:

float[] c = a[] + b;

create a new array.  The semantics of what this would do are unambiguous, it
doesn't interact poorly (or at all) with the rest of the language in any way I can
think of, the fact that it's not allowed is very surprising to newbies, and it's
highly convenient.  Why force people to write boilerplate code if there's no
tradeoff and you're not gaining anything in return?

Also, see bug 2549 (http://d.puremagic.com/issues/show_bug.cgi?id=2549).  If for
some reason we don't allow the creation of new arrays using array ops, we need to
fix the error detection.



More information about the Digitalmars-d mailing list