Forum > General

[Solved] Generic record enumerator in Delphi mode

(1/3) > >>

Okoba:
I am trying to write this FPC mode code in the Delphi mode, mostly because I do not like to write generic and specialize in every place and in a real world sample it will a lot and code will look somewhat ugly.

--- 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";}};} ---unit Test3; {$mode ObjFPC}{$H+}{$modeswitch advancedrecords} interface type  generic TTest3Enumerator<T> = record    FValue: Pointer;    FCurrent: Integer;    function GetCurrent: T;    function MoveNext: Boolean;    property Current: T read GetCurrent;  end;   generic TTest3<T> = record  type    TTest3EnumeratorSpec = specialize TTest3Enumerator<T>;  var    Value: array of T;    function GetEnumerator: TTest3EnumeratorSpec;  end; implementation function TTest3.GetEnumerator: TTest3EnumeratorSpec;begin  Result.FCurrent := -1;  Result.FValue := @Self;end; function TTest3Enumerator.GetCurrent: T;begin  Result := specialize TTest3<T>(FValue^).Value[FCurrent];end; function TTest3Enumerator.MoveNext: Boolean;begin  FCurrent := FCurrent + 1;  Result := FCurrent < Length(specialize TTest3<T>(FValue^).Value);end; end.
The code in Delphi mode:


--- 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";}};} ---unit Test5; {$mode Delphi} interface type  TTest5Enumerator<T> = record    FValue: Pointer;    FCurrent: Integer;    function GetCurrent: T;    function MoveNext: Boolean;    property Current: T read GetCurrent;  end;   TTest5<T> = record  type    TTest5EnumeratorSpec = TTest5Enumerator<T>;  var    Value: array of T;    function GetEnumerator: TTest5EnumeratorSpec;  end; implementation function TTest5.GetEnumerator: TTest5EnumeratorSpec; //<<Errorbegin  Result.FCurrent := -1;  Result.FValue := @Self;end; function TTest5Enumerator.GetCurrent: T;begin  Result := TTest5<T>(FValue^).Value[FCurrent];end; function TTest5Enumerator.MoveNext: Boolean;begin  FCurrent := FCurrent + 1;  Result := FCurrent < Length(TTest5<T>(FValue^).Value);end; end.
And the error :

--- Quote ---test5.pas(26,10) Error: Class identifier expected
--- End quote ---

Suggestion about why the error?

avk:
It seems there should be something 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";}};} ---type  TTest5Enumerator<T> = record    FValue: Pointer;    FCurrent: Integer;    function GetCurrent: T;    function MoveNext: Boolean;    property Current: T read GetCurrent;  end;   TTest5<T> = record  type    TTest5EnumeratorSpec = TTest5Enumerator<T>;  var    Value: array of T;    function GetEnumerator: TTest5EnumeratorSpec;  end; implementation function TTest5<T>.GetEnumerator: TTest5EnumeratorSpec;begin  Result.FCurrent := -1;  Result.FValue := @Self;end; function TTest5Enumerator<T>.GetCurrent: T;begin  Result := TTest5<T>(FValue^).Value[FCurrent];end; function TTest5Enumerator<T>.MoveNext: Boolean;begin  FCurrent := FCurrent + 1;  Result := FCurrent < Length(TTest5<T>(FValue^).Value);end;   

PascalDragon:
Delphi supports type overloads for generics (e.g. TFoo<T> and TFoo<S, T> both inside the same unit). Thus you need to use that as well when providing a method implementation as avk showed (e.g. procedure TTest5Enumerator<T>.GetCurrent: T and so on).

Okoba:
Thanks to both of you.
@PascalDragon, is there any plan to allow this simple style of Generic in Delphi or a compiler condition to activate it? I like FPC mode much more (eg AutoDeref-) but generics makes me use Delphi mode and change almost all things back to FPC mode.

marcov:

--- Quote from: OkobaPatino on June 17, 2021, 02:39:13 pm --- much more (eg AutoDeref-) 

--- End quote ---

use
{$mode delphi}{$pointermath on}

Navigation

[0] Message Index

[#] Next page

Go to full version