Delphi mode generic function in expression - compile errors lazarus / 64-FPC development ---------------------------------------------------------------------- alpine · 2022-08-27 08:48 I wrote a generic compare function for sorting and when I've decided to invert the sign the compiler gave an error. Here is a code illustrating the problem: 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 program Project1;{$MODE Delphi} function Fun(X: T): Integer;beginend; var  I: Integer;begin  I := Fun(1);  I := Fun(1) * -1; // Fatal: Syntax error, ";" expected but "*" found  I := -1 * Fun(1); // Error: Internal error 2015071704end. Why is this happening? Lazarus 2.2.2 (rev lazarus_2_2_2) FPC 3.2.2 i386-win32-win32/win64 ---------------------------------------------------------------------- Thaddy · 2022-08-27 09:10 I can make both disappear, but note that an internal error should always be reported on the bug tracker. I suppose a fix for the internal error will also fix the first one. They are somewhat related as below shows. Work Around code :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 {$MODE Delphi} function Fun(X: T): Integer;begin  Result :=1; // just in case the compiler can not work without a result value... Both errors still appear, thoughend; var  I: Integer;begin  I := Fun(1);  I := (Fun(1)) * -1; // Makes disappear Fatal: Syntax error, ";" expected but "*" found  I := -1 * (Fun(1)); // Makes disappear Error: Internal error 2015071704end. ---------------------------------------------------------------------- alpine · 2022-08-27 09:22 Yes, that is the first thing to try that comes into mind. Also I found this: https://forum.lazarus.freepascal.org/index.php/topic,55039.msg40 9402.html#msg409402 with a similar example. But this one is a pretty simpler for an internal error to happen. ---------------------------------------------------------------------- Thaddy · 2022-08-27 09:52 It seems that multiplication by a const is evaluated by the compiler before the specialization is complete. If that is the case, the compiler cannot know if a multiplication is even possible with generics. IOW the specialization comes too late in mode delphi. But indeed, although the subject you link to isn't quite the same, using mode objfpc works: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 {$MODE objfpc} generic function Fun(X: T): Integer;begin  Result :=1;end; var  I: Integer;begin  I := specialize Fun(1);  I := specialize Fun(1) * -1;   I := -1 * specialize Fun(1); end.I know you knew that, but just to clarify for other readers. ---------------------------------------------------------------------- Thaddy · 2022-08-27 10:12 Btw, you can factor out the objfpc part and the specialization into a separate unit, so the rest of the program can still use the delphi way. ---------------------------------------------------------------------- alpine · 2022-08-28 16:14 Issue created: https://gitlab.com/freepascal.org/fpc/source/-/issues/39881 thanks to Serge Anvarov who managed to do it over the weekend for me ---------------------------------------------------------------------- PascalDragon · 2022-08-28 16:17 Quote from: y.ivanov on August 27, 2022, 10:48:45 amI wrote a generic compare function for sorting and when I've decided to invert the sign the compiler gave an error. Here is a code illustrating the problem: 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 program Project1;{$MODE Delphi} function Fun(X: T): Integer;beginend; var  I: Integer;begin  I := Fun(1);  I := Fun(1) * -1; // Fatal: Syntax error, ";" expected but "*" found  I := -1 * Fun(1); // Error: Internal error 2015071704end. Why is this happening? The compiler simply does not support more complex expressions involving inline specializations in mode Delphi and it will be a long time till all those issues will be fixed. If you want to use such then use mode ObjFPC. ---------------------------------------------------------------------- alpine · 2022-08-28 16:29 Quote from: PascalDragon on August 28, 2022, 06:17:52 pm*snip* The compiler simply does not support more complex expressions involving inline specializations in mode Delphi and it will be a long time till all those issues will be fixed. If you want to use such then use mode ObjFPC. I will. To be fair, I'm fine with the parenthesis as well. It was an improvement over old Delphi converted unit and I felt it will be good to share it.