[fpc-devel] Proposal: Enhanced replacement for assignment operators

Alexander Klenin klenin at gmail.com
Fri Aug 6 20:28:13 CEST 2010


On Sat, Aug 7, 2010 at 04:59, "Vinzent Höfler"
<JeLlyFish.software at gmx.net> wrote:
> Alexander Klenin <klenin at gmail.com>:
>
>> > But you can mistake the "+=" for a ":=". Replacing one possible reading
>> error with another isn't much of an improvement.
>>
>> You can also mistake "." for ",", ">=" for "<=", or mix up "<>", "><",
>> "<<" and ">>" --
>> which are all valid but different symbols in FPC. This is quite a minor
>> problem.
>
> Except for the ">=" "<=" the resulting error would not compile in most cases. THAT's the difference.

I thought we were talking about reading errors, not writing errors?

>> Code bloat/duplication is much more serious.
>> It is usually not just SomeReallyLongVariableNameY, but something like:
>> SomeFunctionReturningObject(param1, param2).PointArrayField[i] +=
>> FunctionReturningPointOffset(param);
>> Try rewriting that without "+=".
>
> Add (SomeFunctionReturningObject (param1, param2).PointArrayField[i],
>     FunctionReturningPointOffset (param));

Exactly, and so we are back to square one -- you accumulate integers with "Inc",
points with "AddPoint", doubles with "FloatInc", write extraneous
four-line wrapper for every type and always forget which of
MultVector and VecMultiply is procedure, and which is function.
BTW, the above is almost-real example from the code base I worked with.

> IMO, since the invention of subroutines, the expressive power of operators is overestimated. Consistently.

Ok, here is a real code from one of my Delphi 7 projects:

  mu :=
    MatrixMultiplyVector(
      InverseCoordMatrix(
        CoordMatrixAdd(
          CoordMatrixScale(dst.A, w), CoordMatrixScale(ASource.A, AWeight)
        ),
      ),
      VectorAdd(
        VectorScale(MatrixMultiplyVector(dst.A, dst.Mu), w),
        VectorScale(
          MatrixMultiplyVector(ASource.A, ASource.Mu), AWeight)
      ),
    );
  a :=
    CoordMatrixScale(
      CoordMatrixAdd(
        CoordMatrixScale(dst.A, w), CoordMatrixScale(ASource.A, AWeight)
      ),
      1 / (w + AWeight)
    );

Here is the same code after upgrading:

  mu :=
      InverseCoordMatrix(w * dst.A + AWeight * ASource.A) *
      (w * dst.A * dst.Mu + AWeight * ASource.A * ASource.Mu);
  a := w * dst.A + AWeight * ASource.A / (w + AWeight);

[Both versions slightly simplified to avoid overbloating the message].
Believe me, expressive power of operators is severely underestimated
by those who did not try to do calculations without them ;-)

-- 
Alexander S. Klenin



More information about the fpc-devel mailing list