(snip on array assignment and array expressions)
1) Operator overloading doesn't break existing code at all.
2) It is implemented in a C compiler since 2004-2005
3) It allows by overloading the operator [ ] to construct
arrays with roperties as the user wishes. This is not "A WAY" of
doing array assignment, implementing read only arrays, copy on
write arrays, etc, it is a method that can be used to implement all
those solutions in a compatible way.
But there is no blinder person as the man that doesn't want to see.
OK, we have a slightly different perspective on these things: since 1980s,
I've been using a two tiers of languages (sometimes, 3!): a higher level one
to do the bulk of application programming, and a lower level C-like one to
implement everything else, including the other language. (And recently that
has actually been C-based.)
You I know prefer to use C for everything, so need all these advanced
features.
However operator overloading will affect the transparency for which C is
famous. And you won't be able to go too far without needing to use garbage
collection (to deal with intermediate results, slices and pointers to
intermediate results and so on).
OK, but say you don't go that far.
I do remember PL/I compilers warning that temporary arrays were needed
to evaluate an expression. Nice of them to warn about it.
As far as I know, the most common array expression in languages that
have them is assigning a scalar to every element of an array.
Without thinking too hard about it, ALGOL has the := assignment
operator, so consider the possibility of using that as an array
assignment operator. Since the compiler has to know the extent of
the array to do it, only for arrays with known extent.
Maybe the next most common array expressions are elementwise addition
and multiplying by a scalar. Also nice and easy to do.
So, one possiblity would be array assignment with :=.
a := 0;
and array addition:
a := b + c; (I think it doesn't need the :+ array addition operator.)
a := 3*d;
If you allow for elemental functions:
y := sqrt(x)
does elementwise sqrt, no temporary needed.
You will also have to start thinking about whether that array
assignment is going to be deep or shallow, because it's
got nested flex arrays, and structs which contain flexible arrays,
and about whether certain types can be mutable or immutable,
because that affects sharing, etc.
OK, so don't allow flex arrays.
There is one complication that comes up fairly early, though, with simple
expressions like:
a := a[10];
In PL/I, assignment is done elementwise, such that the new value of
a[10] is used as soon as it changes. (Convenient for loop expansion.)
Fortran requires that the old value be used for all. That is, the whole
right side is evaluated before any element changes. (If the compiler
verifies, under aliasing rules, that no such changes occur, it can
evaluate and assign element by element.)
The PL/I rule is probably more applicable to C. That is, you get the
same result as a for loop expansion, without writing the loops.
(Though slightly less convenient on vector processors.)
It can get complicated very quickly.
Often enough, someone asks on comp.lang.fortran how to write some
complicated operation using array expressions and no DO loops.
Usually in the cases people ask about, the result is much more
complicated, and likely slower, than writing the loops.
But most of those complicated cases require some array reduction
intrinsic functions that C doesn't have.
-- glen