Is it possible to replace an operator (Currency again) lazarus / 62-General ---------------------------------------------------------------------- BrunoK · 2019-12-28 11:57 I wanted to override some operators for Currency. So I tried : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TCurrencyHelper = type Helper for Currency  public    function FormatCurr : string;  public    class operator + (constref aCurL : currency; constref aCurR : Currency) : Currency;  end; but it  complains with : QuoteCompilation du projet - Cible : pgmCurrencyHelper.exe : Code de sortie 1 - Erreurs : 2 pgmCurrencyHelper.pas(20,11) Error: Procedure or Function expected pgmCurrencyHelper.pas(20,11) Fatal: Syntax error, "identifier" expected but "OPERATOR" found on the line where class operator is declared. Is there another way to override the base type operators ? ---------------------------------------------------------------------- jamie · 2019-12-28 17:52 I can't see all of this is supported. http://docwiki.appmethod.com/appmethod/1.15/topics/en/Operator_Overloa ding_(Object_Pascal) But it needs to be inside a Record.. ---------------------------------------------------------------------- winni · 2019-12-28 18:39 Hi! As @jamie  said: you have to use a record: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   type  TMyRecord = record    C: currency;  end; operator + (const C1, C2: TMyRecord): TMyRecord;   And the implementation is this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i operator + (const C1, C2: TMyRecord): TMyRecord;begin  Result.C := C1.C + C2.C;end;  Winni ---------------------------------------------------------------------- MarkMLl · 2019-12-28 20:23 Quote from: winni on December 28, 2019, 07:39:58 pmoperator + (const C1, C2: TMyRecord): TMyRecord; I was wondering about that... I've certainly redefined operators for dynamic arrays but will it work for objects? MarkMLl ---------------------------------------------------------------------- howardpc · 2019-12-28 20:52 Quote from: MarkMLl on December 28, 2019, 09:23:35 pmI was wondering about that... I've certainly redefined operators for dynamic arrays but will it work for objects? If you mean classes, Yes. However, you have to be clear what you expect the result of adding one class instance to another might mean. For classes that contain only numeric data the meaning is perhaps straightforward. Though does the addition create a new instance, or merely add the value(s) of the first instance to those of the second, and return the amended second instance? Other non-numeric cases of class addition/multiplication etc. are are less intuitively clear. ---------------------------------------------------------------------- jamie · 2019-12-28 21:49 I believe what is being sought out is to override the intrinsic + function of the compiler for currency.  So this means no DOT operations etc...  I don't believe they will succeed in this quest for such a operation that hides the intrinsic version of it.  The best they can hope for is maybe creating a type from Currency and then making a helper for that , maybe.. Type  TMyCurrency = Currency; and then from there, Maybe, just maybe something could be done with that. ---------------------------------------------------------------------- jamie · 2019-12-28 22:05 Ok, this seems to work Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TMyCurrency = Record   Enity :Currency;   Class Operator + (L,R:TMyCurrency):TMyCurrency; end;        ImplementationClass Operator TMyCurrency.+(L,R:TMyCurrency):TMyCurrency;Begin  result.Enity := L.Enity+R.Enity;end;                                       Of course you need to ether Type Case to a Currency for the rest of the math or just operate on all the other types too... I guess I was late coming in with this, personnaly I don't know what the real issue is ? I've used currency in its current state and I have not yet seen an issue unless it is being used on the borders? I think we should have a Fixed Point math type, its basically the same as currency just a different name! ---------------------------------------------------------------------- wp · 2019-12-28 22:18 I don't understand: what is the purpose of overloading an operator for a base type such as Currency? Well, replacing the addition by a multiplication would make sense to let the deposit of a bank account grow extremely fast... ---------------------------------------------------------------------- winni · 2019-12-28 22:30 Hi! I Think he wanted to have - as spin off - a nice formated curreny string with thousand-separators. Look at his helper definition in #1. Or another problem: How simple does this look when you write: brutto := netto +VAT; It looks much nicer for the boss if you do it like @Jamie: You got a record, a class and an operator! Wow! Winni ---------------------------------------------------------------------- BrunoK · 2019-12-29 07:09 Quote from: wp on December 28, 2019, 11:18:38 pmI don't understand: what is the purpose of overloading an operator for a base type such as Currency? Well, replacing the addition by a multiplication would make sense to let the deposit of a bank account grow extremely fast...    Actually there  is no problem with currency on i386 that use x87 FPU. But x86_64 multiplication and divisions are a catastrophe when having values nearing currency domain limits. So l'm evaluating how to circumvent thé anoying 'glitches'. ---------------------------------------------------------------------- MarkMLl · 2019-12-29 10:40 Quote from: BrunoK on December 29, 2019, 08:09:15 amQuote from: wp on December 28, 2019, 11:18:38 pmI don't understand: what is the purpose of overloading an operator for a base type such as Currency? Actually there  is no problem with currency on i386 that use x87 FPU. But x86_64 multiplication and divisions are a catastrophe when having values nearing currency domain limits. So l'm evaluating how to circumvent thé anoying 'glitches'. Also representing currency as an object (etc.) opens the possibility of representing /which/ currency it was received or disbursed as, the conversion rate assumed at the date the entry went into the journal, and which country's rounding regulations were applied. MarkMLl ---------------------------------------------------------------------- jamie · 2019-12-29 16:45 Quote from: BrunoK on December 29, 2019, 08:09:15 amQuote from: wp on December 28, 2019, 11:18:38 pmI don't understand: what is the purpose of overloading an operator for a base type such as Currency? Well, replacing the addition by a multiplication would make sense to let the deposit of a bank account grow extremely fast...    Actually there  is no problem with currency on i386 that use x87 FPU. But x86_64 multiplication and divisions are a catastrophe when having values nearing currency domain limits. So l'm evaluating how to circumvent thé anoying 'glitches'. Yeah, I have that problem too every time I go to the bank and do my weekly deposits