[fpc-pascal] Re: operator overloading and counting references / creating / destoying anonymous instances

Bernd prof7bit at googlemail.com
Fri Jul 29 13:43:58 CEST 2011


2011/7/29 Jürgen Hestermann <juergen.hestermann at gmx.de>:
> Bernd schrieb:
>> Occasionally I hear other people mentioning operator overloading as a
>> must-have feature of any decent language but I wonder what real-world
>> problems they are actually solving with it.
>
> I think operator overloading is a pain. As you said: What is the advantage?
> For me operators should be defined by the language only (Pascal) so that
> everybody who reads the code imediately knows what happens. Instead you now
> need additional information (that can be far away from the code lines you
> review). That makes code obscure and less clear. But now there is no way
> back. It's implemented. Pascal moves in C direction...

I'm not condemning it per se. It has its uses. I can still overload at
least the comparison operators for example, I didn't want to rant
against operator overloading in general. I only think it is overrated
and often more complicated than useful.

If I have a type that can be created and deleted/overwritten on the
stack on the fly (record, object) without needing a destructor to free
additional resources then everything can be made to work as if it were
a natural part of the language and there would be no performance
penalty and everything would be fine.

My question was only if there are really many different real-world
examples that go beyond the complex numbers example and maybe some
simple linear algebra types, that can fit their entire data into a
record on the stack without needing any allocation/deallocation of
heap or external resources. Making a nice looking lightweight wrapper
around an external library would surely be very desirable but I can't
see any way to overcome the problems and these seem fundamental
problems to me. This seems to need some non-trivial optimizations that
might even need help from the compiler itself to make it really
effective.

With interfaces and their reference counting it can be made to work
but the cost of doing this seems so immense that I don't believe it is
justifiable in many real world applications (at least not in my
application).

Maybe if there were a way to make the compiler generate code to hook
into whenever a record is created/deleted/overwritten on the stack
this would allow some creative hacks around some of these problems but
with classes and interfaces alone I can't justify the immense overhead
that is associated with it.



More information about the fpc-pascal mailing list