Overloading for "if" etc. lazarus / 62-General ---------------------------------------------------------------------- MarkMLl · 2022-03-08 08:40 I've just converted some lexing functions to return (in effect) "yes", "no" and "maybe" where the last of those is rarely used. Is there a single operator that can be overloaded that will allow a simple "if" to work? Existing code tends lo look like 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     if not Expect_Literal(defs, 'end', true) then      EXIT_ERROR;    if not Expect_Literal(defs, '.') then      EXIT_ERROR;    Discard_Cruft(defs);  and so on. Updated: I seem to have managed this by defining about a dozen assignment, comparison and logical operators... being careful to define pairs where appropriate since I can't remember how well the compiler swaps operands. I'm still interested to know whether there's a single shortcut I could have used. MarkMLl ---------------------------------------------------------------------- PascalDragon · 2022-03-08 12:26 Quote from: MarkMLl on March 08, 2022, 09:40:47 amUpdated: I seem to have managed this by defining about a dozen assignment, comparison and logical operators... being careful to define pairs where appropriate since I can't remember how well the compiler swaps operands. I'm still interested to know whether there's a single shortcut I could have used. I think an implicit assignment operator to Boolean should do it... ---------------------------------------------------------------------- MarkMLl · 2022-03-08 12:41 Quote from: PascalDragon on March 08, 2022, 01:26:07 pmQuote from: MarkMLl on March 08, 2022, 09:40:47 amUpdated: I seem to have managed this by defining about a dozen assignment, comparison and logical operators... being careful to define pairs where appropriate since I can't remember how well the compiler swaps operands. I'm still interested to know whether there's a single shortcut I could have used. I think an implicit assignment operator to Boolean should do it... I believe it does for  if  , and that echoes my previous experience that an overloaded assignment could get called at unexpected times... is it possible to do something like System.:= or does one have to use Move()? :-) In the end I defined 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 := (s: TSatis): boolean;operator = (s: TSatis; b: boolean): boolean;operator = (b: boolean; s: TSatis): boolean;operator or (s: TSatis; b: boolean): boolean;operator or (b: boolean; s: TSatis): boolean;operator or (s1, s2: TSatis): boolean;operator not (s: TSatis): boolean;operator and (s: TSatis; b: boolean): boolean;operator and (b: boolean; s: TSatis): boolean;operator and (s1, s2: TSatis): boolean;  I definitely needed at least some of those Boolean operations, but I've not explored whether the compiler will swap operands so that one of each pair is effectively redundant... I suspect it would not be a safe thing to rely on. MarkMLl ---------------------------------------------------------------------- PascalDragon · 2022-03-09 12:35 Quote from: MarkMLl on March 08, 2022, 01:41:12 pmI believe it does for  if  , and that echoes my previous experience that an overloaded assignment could get called at unexpected times... is it possible to do something like System.:= or does one have to use Move()? :-) You need to take care yourself that you don't have recursive calls. However if an internal operator is defined (e.g. assignment between records) then you can't overload that anyway (well, okay, with records you can use the new management operators, but that merely hooks into the normal assignment operator). So if the compiler uses an operator where you didn't expect it, then it's more often than not a user error anyway. Quote from: MarkMLl on March 08, 2022, 01:41:12 pmI definitely needed at least some of those Boolean operations, but I've not explored whether the compiler will swap operands so that one of each pair is effectively redundant... I suspect it would not be a safe thing to rely on. It's best to implement them fully anyway, cause oneself might be able to implement them more efficiently than the compiler reusing some other operator (and I think currently we don't do that except for Explicit assignment reusing the non explicit one if the former isn't declared, but it could potentially change in the future). ---------------------------------------------------------------------- MarkMLl · 2022-03-09 13:09 Quote from: PascalDragon on March 09, 2022, 01:35:33 pmSo if the compiler uses an operator where you didn't expect it, then it's more often than not a user error anyway. Always the safest assumption :-) I suspect that particularly in the case of overloaded assignments an "are we recursing?" assertion is probably good practice. QuoteIt's best to implement them fully anyway, cause oneself might be able to implement them more efficiently than the compiler reusing some other operator (and I think currently we don't do that except for Explicit assignment reusing the non explicit one if the former isn't declared, but it could potentially change in the future). Understood and TFT. I'm completely OK with the idea of coding this stuff rather than having excessive "smarts" in the compiler, and that's particularly the case with type conversions. MarkMLl