Forum > General

Advance Record operators calling the wrong function

(1/3) > >>

ad1mt:
I've working on a library to provide a new type, which is using advanced records.
Suddenly, operators like minus and greater-than are calling the wrong function.
For example, I have a record type like this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---T1 = record  private  var v: array of integer;  public  class operator subtract(const v1,v2:T1):T1;  class operator >=(const v1,v2:T1):T1;  end;class operator subtract(const v1,v2:T1):T1; // implementationclass operator >=(const v1,v2:T1):T1; // implementationThen, later in the code, when the following code is executed

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var v1,v2:T1;beginv1:= 10; v2:= 5;v3:= (v1 - v2);at line 4, the >= function is called when I step through the code (when the subtract function should be called), then the value returned makes no sense, and the calling code fails.
I know its unlikely anyone will be able to help, but my head is spinning, and I've got no idea how to proceed.
The only clue I have is that the problem appeared to start when the definition of the advanced record was changed. It used to be like this

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---T1 = record  private  var v: array[0..3] of integer;  public  class operator subtract(const v1,v2:T1):T1;  class operator >=(const v1,v2:T1):T1;  end;class operator subtract(const v1,v2:T1):T1; // implementationclass operator >=(const v1,v2:T1):T1; // implementationWith the new definition (at top) I had to start initialising the newly created type T1 vars with setlength on the internal array.

Thaddy:
You are mixing up Delphi syntax with Objfpc syntax, you use T1.subtract(Delphi) instead of T1.- (objfpc).
Here's a correct example for mode objfpc:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program testop;{$mode objfpc}{$modeswitch advancedrecords} type  T1 = record    v:integer;    class operator initialize(var value:T1);    class operator - (const a,b:T1):T1;    class operator >=(const a,b:T1):Boolean;  end;    class operator T1.initialize(var value:T1);  begin    value := Default(T1);  end;     class operator T1.- (const a,b:T1):T1;  begin    result.v := a.v-b.v;  end;    class operator T1.>=(const a,b:T1):Boolean;  begin    result := a.v >= b.v;  end; begin end.
In mode Delphi you must replace >= with GreaterThanOrEqual:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode delphi} type  T1 = record    v:integer;    class operator initialize(var value:T1);    class operator Subtract (const a,b:T1):T1; // on use this is - (minus)    class operator GreaterthanOrEqual(const a,b:T1):Boolean;// on use this is >=  end;    class operator T1.initialize(var value:T1);  begin    value := Default(T1);  end;     class operator T1.Subtract (const a,b:T1):T1;  begin    result.v := a.v-b.v;  end;    class operator T1.GreaterThanOrEqual(const a,b:T1):Boolean;  begin    result := a.v >= b.v;  end; begin end.

You can not mix the mode specific syntax in a single unit. In this case I prefer objfpc syntax.
I also added auto initialize using a management operator{ you do not have to call setlength anymore if you add that to your code. Default() makes sure your static array is initialized. Not sure this works on a real Delphi..... I believe their syntax slightly differs.

ad1mt:

--- Quote from: Thaddy on December 03, 2023, 12:42:21 pm ---You are mixing up Delphi syntax with Objfpc syntax
You can not mix the mode specific syntax in a single unit.

--- End quote ---
Thanks for this suggestion.
I've been unable to find the advanced record operator definition syntax in the PDF docs. Do you know where to find it?
What I've had to do is set delphi mode at the top, then see which syntax works by trial and error; which is not the best method.
EDIT - I've found the operator list, but I can't find the syntax used for advanced records

Thaddy:
The delphi syntax is available in the online delphi documentation:
https://docwiki.embarcadero.com/RADStudio/Sydney/en/Operator_Overloading_(Delphi)
and the Freepascal syntax is available in the FreePascal documentation https://www.freepascal.org/docs-html/ref/refch15.html

Btw: my code is not a suggestion but an explanation about the difference in syntax. (mandatory)

ad1mt:
I don't think this needs too much investigation... I went back to a previous stable version, and have started re-applying the changes.
So far, the problem has not re-appeared, so it looks like it might be user error    :o

Navigation

[0] Message Index

[#] Next page

Go to full version