Re: [fpc-pascal] Re: operator overloading and counting references / creating / destoying anonymous instances
Henry Vermaak schrieb: I think operator overloading is a pain. As you said: What is the advantage? For me operators should be defined by the language only It improves readability, making it more logical. Just the opposite! It hides important imformation for the reading person. Say for instance you are working on Galois fields and you have to do arithmetic on the elements like this: g1 + g2 / g3 If you don't have operator overloading, you have to do it with functions, like this: gf_add(g1, gf_div(g2, g3)) This is not very readable, I'm sure you will agree. No, I don't agree at all. The procedure call clearly shows me that a complex calculation is done instead of an (atomic) add and division command on numbers. For me the basic operators should be just that: Basic operators. Otherwise you have problems to interpret code. For example, IMO the assignment := should just copy bytes, nothing more. There can be additional range checks but it should not lead to side effects you are not aware of. I had a hard time to find out that a simple A:=nil on a dynamic array does more than just copying bytes to the memory adress of A. As a side effect it uses the current pointer in A and frees memory. But I had just read this data from a file so that the pointer was garbage. I just wanted to init A. But what a surprise that this atomic assignment command does more on dynamic arrays! If I create pointers to types on my own, this does not happen. Operator overloading weakens the meaning of the operators. It can be just everything but you do not see it anymore. clear. But now there is no way back. It's implemented. Pascal moves in C direction... Troll. C has no operator overloading. I meant it in a more general spirit: Pascal was created to be clear and unambiguous while C was an improved assembler which ignored any kind of readability. Now all these obscure things creep into Pascal too so that in this respect there is not much difference between current C(++) and current Pascal compilers anymore. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: operator overloading and counting references / creating / destoying anonymous instances
Say for instance you are working on Galois fields and you have to do arithmetic on the elements like this: g1 + g2 / g3 If you don't have operator overloading, you have to do it with functions, like this: gf_add(g1, gf_div(g2, g3)) This is not very readable, I'm sure you will agree. No, I don't agree at all. The procedure call clearly shows me that a complex calculation is done instead of an (atomic) add and division command on numbers. And there is another advantage of using procedures/functions instead of overloading operators: You can search for the procedure to look what it actualy does. How do you find the code that overloaded an operator? ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: operator overloading and counting references / creating / destoying anonymous instances
Am 30.07.2011 17:05, schrieb Jürgen Hestermann: Say for instance you are working on Galois fields and you have to do arithmetic on the elements like this: g1 + g2 / g3 If you don't have operator overloading, you have to do it with functions, like this: gf_add(g1, gf_div(g2, g3)) This is not very readable, I'm sure you will agree. No, I don't agree at all. The procedure call clearly shows me that a complex calculation is done instead of an (atomic) add and division command on numbers. And there is another advantage of using procedures/functions instead of overloading operators: You can search for the procedure to look what it actualy does. Not if you use function/procedure overloading. Then the situation is exactly the same as for operators. How do you find the code that overloaded an operator? ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: operator overloading and counting references / creating / destoying anonymous instances
Am 29.07.2011 13:43, schrieb Bernd: 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). The automatic constructor/destructor concept of C++ causes the same overhead. And the overhead for a function or operator overloading based approach is the same imo. Or do you have any example where a function based approach performs better? Overloaded operators are converted into function calls, there is no real difference. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: operator overloading and counting references / creating / destoying anonymous instances
And there is another advantage of using procedures/functions instead of overloading operators: You can search for the procedure to look what it actualy does. Not if you use function/procedure overloading. Then the situation is exactly the same as for operators. Repeating myself, if there isn't something like CodeTools (i.e. only with text search) is way more difficult to search for operator declarations. How do you find the code that overloaded an operator? -Flávio ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: operator overloading and counting references / creating / destoying anonymous instances
2011/7/30 Florian Klaempfl flor...@freepascal.org: The automatic constructor/destructor concept of C++ causes the same overhead. And the overhead for a function or operator overloading based approach is the same imo. Or do you have any example where a function based approach performs better? Overloaded operators are converted into function calls, there is no real difference. I didn't intend to mean it is better solved in C++ (or any other language), I didn't want to specifically criticize the Operator overloading how it is implemented in Object Pascal, I just spent some time experimenting with trying to wrap this bignum library in some clever way to make it look and behave nicely. I posted this thread only because at some point I realized that whatever I do to make it behave like a built in type it becomes slow and ineffective and the wrapper becomes big and complicated. The reason is because it IS a function call. A := B + C; A := add(B, C); This will create a new A and free the old one. I have no idea how to prevent this. add(A, B, C); with var or out arguments would enable me to re-use the existing A. If I cannot find a way to let me look to the left *trough* the := operator from inside the + operator I have no Idea how to implement something like this, so I always have to create a new instance of A and let the old one be freed. The solution that I have chosen now will force me to explicitly create and free them manually and all my methods take all variables as arguments, have side effects and don't return anything. The only operators that I have overridden are the comparison operators. Here is the unit I am talking about in its current form (this is at least the fifth different variation/rewrite of it already and likely to change yet again once I have a new idea): http://code.google.com/p/fpbitcoin/source/browse/trunk/openssl_bignum.pas and here is an example where it is used (currently the only place because it is all still far from complete): http://code.google.com/p/fpbitcoin/source/browse/trunk/bitcoin_base58.pas And since I believe this is a very deep and fundamental problem and not only related or limited to FPC and not easily solved without massive changes to the compiler and the entire operator overloading syntax (if it is possible at all) I did not intend to make this a Pascal bashing thread. I LOVE this language and this compiler and the related projects around it. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: operator overloading and counting references / creating / destoying anonymous instances
if i understand correctly, your problem is not operator overloading or whatever in the language. You have references. Now you want to write counted references to references and deal with this in the overloaded operators your problem is right into the counted reference to reference. A := B makes A point to the same reference as B, and this is perfectly correct semantic, but this will not work correctly by itself if the referenced object is itself a reference (without counting) to another thing. a pointer to a pointer... you have to refcount the original pointer instead. So the solution is to have a singleton that holds all references to all bignums and then work from that on. 2011/7/30 Bernd prof7...@googlemail.com: 2011/7/30 Florian Klaempfl flor...@freepascal.org: The automatic constructor/destructor concept of C++ causes the same overhead. And the overhead for a function or operator overloading based approach is the same imo. Or do you have any example where a function based approach performs better? Overloaded operators are converted into function calls, there is no real difference. I didn't intend to mean it is better solved in C++ (or any other language), I didn't want to specifically criticize the Operator overloading how it is implemented in Object Pascal, I just spent some time experimenting with trying to wrap this bignum library in some clever way to make it look and behave nicely. I posted this thread only because at some point I realized that whatever I do to make it behave like a built in type it becomes slow and ineffective and the wrapper becomes big and complicated. The reason is because it IS a function call. A := B + C; A := add(B, C); This will create a new A and free the old one. I have no idea how to prevent this. add(A, B, C); with var or out arguments would enable me to re-use the existing A. If I cannot find a way to let me look to the left *trough* the := operator from inside the + operator I have no Idea how to implement something like this, so I always have to create a new instance of A and let the old one be freed. The solution that I have chosen now will force me to explicitly create and free them manually and all my methods take all variables as arguments, have side effects and don't return anything. The only operators that I have overridden are the comparison operators. Here is the unit I am talking about in its current form (this is at least the fifth different variation/rewrite of it already and likely to change yet again once I have a new idea): http://code.google.com/p/fpbitcoin/source/browse/trunk/openssl_bignum.pas and here is an example where it is used (currently the only place because it is all still far from complete): http://code.google.com/p/fpbitcoin/source/browse/trunk/bitcoin_base58.pas And since I believe this is a very deep and fundamental problem and not only related or limited to FPC and not easily solved without massive changes to the compiler and the entire operator overloading syntax (if it is possible at all) I did not intend to make this a Pascal bashing thread. I LOVE this language and this compiler and the related projects around it. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: operator overloading and counting references / creating / destoying anonymous instances
Imagine the following : A - C - E B - D - E A is ref counted, and says that theres 10 references to the object C B is ref counted, and says that theres 5 references to the object D But both C and D points to the same object ! So now you have actually 15 references to the same object. Lets say objects gone out of scope and C is now counted at 0 on A, what to do ? Deallocate C causes E to be deallocated, but, what happens at B - D ? So what you need ? A - Singleton - E B - Singleton - E In other words, make an object that stores all bignums and decides when to allocate or deallocate then and work from that on. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: operator overloading and counting references / creating / destoying anonymous instances
Am 30.07.2011 19:27, schrieb Bernd: 2011/7/30 Florian Klaempfl flor...@freepascal.org: The automatic constructor/destructor concept of C++ causes the same overhead. And the overhead for a function or operator overloading based approach is the same imo. Or do you have any example where a function based approach performs better? Overloaded operators are converted into function calls, there is no real difference. I didn't intend to mean it is better solved in C++ (or any other language), I didn't want to specifically criticize the Operator overloading how it is implemented in Object Pascal, I just spent some time experimenting with trying to wrap this bignum library in some clever way to make it look and behave nicely. I posted this thread only because at some point I realized that whatever I do to make it behave like a built in type it becomes slow and ineffective and the wrapper becomes big and complicated. The reason is because it IS a function call. A := B + C; A := add(B, C); This will create a new A and free the old one. I have no idea how to prevent this. add(A, B, C); with var or out arguments would enable me to re-use the existing A. This is exactly what ref. counting solves? Did you read my other mail regarding your problem with ref. counting? ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: operator overloading and counting references / creating / destoying anonymous instances
Repeating myself, if there isn't something like CodeTools (i.e. only with text search) is way more difficult to search for operator declarations. Why? Searching for operator+ is no more difficult than for function add ? Blanks and linebreaks... Of course if one always use the same editor, that supports searching for linebreaks, and often use Regex in it, maybe they'll remember whether its Regex flavor uses \b or \s or \w for blanks :-/ Of course this is not major, just annoying, since you'll rarely need it. But maybe if FPC forced the symbol to follow the keyword immediately... ;-) Best regards, Flávio ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: operator overloading and counting references / creating / destoying anonymous instances
2011/7/30 Florian Klämpfl flor...@freepascal.org: This is exactly what ref. counting solves? Did you read my other mail regarding your problem with ref. counting? Yes, of course Ref counting solves it (in the sense that it can all be made to work as expected and not leak memory) and your suggestion of checking the reference counter also solves the problem of detecting when a copy would be needed and when it is safe to recycle the existing object. But this does not solve the initial problem that in most cases with operator overloading I simply do not have any access to the result and *must* create a new instance and in my bignum example this is 3 times slower than intelligently reusing existing objects. A := B + C The code in the plus operator cannot access A to change it even if it were safe to recycle the existing instance. It does not even know about the A, it has no way of looking through the function result to see what is on the other side and check its ref count, and so it *always* must create a new A and let the ref counting dispose the old one. procedure Add(var A, B, C: TBigNum); could check the ref count of A and optimize this greatly and simply recycle the existing instance if it detects that it is the only one. But a function function Add(A, B: TBigNum): TBigNum; does not have any other chance than *always* creating a new instance. Result is always nil, I cannot look through it, its a one-way street, I cannot make any assumptions what the result is used for and cannot check it at runtime. If the operator overloading functions could be defined like this (pseudo code): operator + (var Result: TBigNum; A, B: TBigNum); then maybe but I'm not sure whether it would even be possible to make the compiler arrange things in such a way that such a thing could be done. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: operator overloading and counting references / creating / destoying anonymous instances
Am 30.07.2011 22:54, schrieb Flávio Etrusco: Repeating myself, if there isn't something like CodeTools (i.e. only with text search) is way more difficult to search for operator declarations. Why? Searching for operator+ is no more difficult than for function add ? Blanks and linebreaks... Of course if one always use the same editor, that supports searching for linebreaks, and often use Regex in it, maybe they'll remember whether its Regex flavor uses \b or \s or \w for blanks :-/ Of course this is not major, just annoying, since you'll rarely need it. But the same applies to functions as well? nobody provents you to code like function add(...) : ...; ? Searching for add might not be feasible because the symbol could be used a lot (I know about this because I'am a heavy jump to symbol by grep-user :) But maybe if FPC forced the symbol to follow the keyword immediately... ;-) ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal