Generic class and visibilty of private members lazarus / 21-General ---------------------------------------------------------------------- Bart · 2021-01-15 20:56 Hi, If I derive a specialized class from a generic class (which is defined in a separate unit), then in that class (and instances of that class) I have access to private fields of that class. Is that to be expected? For "normal" classes this is not the case. Given the following unit: 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 unit foo; {$mode objfpc}{$H+} interface uses  Classes, SysUtils; type   { TFoo }   TFoo = class  private    PrivateVar: String;  end;    type  generic TBar=class    private      PrivateVar:T;  end;   TBarInt = specialize TBar; implementation end. And then this program: 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 vis; {$mode objfpc}{$h+} uses foo; type  TBarChar = class (specialize TBar);  TFooChild = class(TFoo); var  AFoo: TFoo;  AFooChild: TFooChild;  ABarChar: TBarChar;  ABarInt: TBarInt;  begin  ABarChar.PrivateVar := 'A';  //compiles (to my surprise: why?)  ABarInt.PrivateVar := 1;     //vis.lpr(21,11) Error: identifier idents no member "PrivateVar" (if the above is allowed, why not this?)  AFooChild.PrivateVar := '';  //vis.lpr(22,13) Error: identifier idents no member "PrivateVar" (as expected)  AFoo.PrivateVar := '';       //vis.lpr(23,8) Error: identifier idents no member "PrivateVar"  (as expected)end. Never mind that the code is bogus and would crash instantly  Bart ---------------------------------------------------------------------- GAN · 2021-01-15 21:28 I'm not an expert on the subject, but I found this: QuoteA private member is invisible outside of the unit or program where its class is declared. In other words, a private method cannot be called from another module, and a private field or property cannot be read or written to from another module. ---------------------------------------------------------------------- Bart · 2021-01-15 21:48 But then why the discrepancy betweenABarChar (child of TBar, declared in main program) and TFooChils (child of TFoo, declared in main program)? Bart ---------------------------------------------------------------------- speter · 2021-01-16 00:50 I believe that "specialize" = create. if you add: 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   tanotherfoo = class(Tfoo)  private    voo : integer;  end;to your program; you can then have: 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 var  afoo2 : tanotherfoo;...  afoo2.voo:=6; So, it's apples and oranges (all fruit). cheers S. ---------------------------------------------------------------------- lainz · 2021-01-16 01:26 I don't know why all use foo and bar. I can't visualize that and I never understood these examples... I prefer TCar. TPerson. ---------------------------------------------------------------------- jamie · 2021-01-16 01:31 Quote from: lainz on January 16, 2021, 02:26:18 amI don't know why all use foo and bar. I can't visualize that and I never understood these examples... I prefer TCar. TPerson.  I like TMaserati  My self ---------------------------------------------------------------------- winni · 2021-01-16 01:47 @lainz Perhaps you can't visualize that . But you can realize that: Let's go to the bar and drink some foo ..... Winni ---------------------------------------------------------------------- speter · 2021-01-16 01:50 [removed off colour humour] ;-) ---------------------------------------------------------------------- Blaazen · 2021-01-16 02:30 Generic class is just a template and TBarChar is derived from TBar which is specialized (i.e. declared) in the same unit (vis), therefore you have access to private fields. It's not a bug IMO. ---------------------------------------------------------------------- PascalDragon · 2021-01-16 10:50 Quote from: Bart on January 15, 2021, 09:56:09 pmIf I derive a specialized class from a generic class (which is defined in a separate unit), then in that class (and instances of that class) I have access to private fields of that class. Regarding terminology: you do not “derive” a specialized class from a generic one, you “specialize” a generic class. And your TBarChar and TBarFoo “derive” from a specialized class (which for all intents and purposes is just a normal class now). Quote from: Bart on January 15, 2021, 09:56:09 pmNever mind that the code is bogus and would crash instantly  It should not work. It currently works, because the compiler is essentially recreating the class inside your main program file thus it's essentially an access from the same unit. As the compiler could in theory however reuse the specialization from another unit the code would then fail. Delphi behaves correctly here. Please report as a bug. ---------------------------------------------------------------------- Bart · 2021-01-16 16:42 Quote from: PascalDragon on January 16, 2021, 11:50:44 amIt should not work. ... Please report as a bug. Reported as issue #38363. Bart