View Issue Details

IDProjectCategoryView StatusLast Update
0033120FPCDocumentationpublic2018-02-24 17:36
ReporterThaddy de KoningAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
PlatformallOSallOS Versionall
Product Version3.1.1Product Build38099 
Target Version3.2.0Fixed in Version3.1.1 
Summary0033120: class var vs class const may need some clarification
DescriptionAlthough the documentation is correct it may need some clarification regarding class var vs class const.
- class var is limited to the class where it is declared and is not inherited by descendants
- class const will be inherited by descendants.

Two entries related:
https://www.freepascal.org/docs-html/ref/refse40.html
https://www.freepascal.org/docs-html/ref/refsu24.html#x72-940006.2.2

There may be more. The difference was obvious to me but not to everyone.
Steps To ReproduceSee discussion and example code here:
http://forum.lazarus.freepascal.org/index.php/topic,39940.msg275139.html#msg275139
TagsNo tags attached.
Fixed in Revision1466.
FPCOldBugId
FPCTarget
Attached Files

Activities

Thaddy de Koning

2018-02-03 12:21

reporter   ~0106186

Last edited: 2018-02-03 12:34

View 3 revisions

Note the difference also holds true in mode {$J+}: the const inherits, the var does not, as implicitly explained in the class property entry.
Which e.g. means that of you change the const in {$J+} mode it is of course changed over the whole inheritance tree.
demo:
{$mode delphi}{$J+}
type
  TA = class // base type
    const CODE: integer = 0;
    class constructor create;
  end;
  
  TB = class(TA)
    class constructor create;
  end;
 
  TC = class(TA)
    class constructor create;
  end;
      
  class constructor TA.Create;
  begin
    CODE := 0;
  end;
  
  class constructor TB.Create;
  begin
    CODE := 1;
  end;

  class constructor TC.Create;
  begin
    CODE := 2;
  end;

begin
 writeln(Ta.Code:2,Tb.Code:2,Tc.code:2);
end.

Thaddy de Koning

2018-02-04 10:42

reporter   ~0106214

Note a class var seems inheritable if it is not *reintroduced*. Then it behaves like a const. Is that intentional?
EX1:
{$mode delphi}{$J+}
type
  
  TA = class // base type
    class var CODE: integer;
    class constructor create;
  end;
  
  TB = class(TA)
    class constructor create;
  end;
 
  TC = class(TA)
    class constructor create;
  end;
      
  class constructor TA.Create;
  begin
    CODE := 0;
  end;
  
  class constructor TB.Create;
  begin
    CODE := 1;
  end;

  class constructor TC.Create;
  begin
    CODE := 2;
  end;

begin
 writeln(Ta.Code:2,Tb.Code:2,Tc.code:2);
end.

EX2:
{$mode delphi}{$J+}
type
  
  TA = class // base type
    class var CODE: integer;
    class constructor create;
  end;
  
  TB = class(TA)
    class var CODE: integer;
    class constructor create;
  end;
 
  TC = class(TA)
    class var CODE: integer;
    class constructor create;
  end;
      
  class constructor TA.Create;
  begin
    CODE := 0;
  end;
  
  class constructor TB.Create;
  begin
    CODE := 1;
  end;

  class constructor TC.Create;
  begin
    CODE := 2;
  end;

begin
 writeln(Ta.Code:2,Tb.Code:2,Tc.code:2);
end.

A const will throw an error when reintroduced.

Michael Van Canneyt

2018-02-24 17:34

administrator   ~0106582

Last edited: 2018-02-24 17:36

View 2 revisions

Documented this. But note that consts and vars are exactly the same in this regard.

The following 2 programs will behave exactly the same:
{$mode objfpc}
type
  TA = class // base type
    class var CODE: integer;
  end;
  TB = class(TA);
  TC = class(TA);

begin
  TA.Code:=0;
  TB.Code:=1;
  TC.Code:=2;
  Writeln(Ta.Code:2,Tb.Code:2,Tc.code:2);
end.

and


{$mode delphi}{$J+}
type
  TA = class // base type
    const CODE: integer = 99;
  end;
  TB = class(TA);
  TC = class(TA);

begin
  TA.Code:=0;
  TB.Code:=1;
  TC.Code:=2;
  Writeln(Ta.Code:2,Tb.Code:2,Tc.code:2);
end.

They will print
 2 2 2

Similarly, the following 2 programs will behave the same:


{$mode delphi}
type
  TA = class // base type
    class var CODE: integer;
  end;
  TB = class(TA)
    Class var code : integer;
  end;
  TC = class(TA)
    Class var code : integer;
  end;

begin
  TA.Code:=0;
  TB.Code:=1;
  TC.Code:=2;
  Writeln(Ta.Code:2,Tb.Code:2,Tc.code:2);
end.

and
{$mode delphi}{$J+}
type
  TA = class // base type
    const CODE: integer = -99;
  end;
  TB = class(TA)
    const code : integer = -98;
  end;
  TC = class(TA)
    Const code : integer = -97;
  end;

begin
  TA.Code:=0;
  TB.Code:=1;
  TC.Code:=2;
  Writeln(Ta.Code:2,Tb.Code:2,Tc.code:2);
end.

They will both print
 0 1 2

in ObjFPC mode the 2nd variant of both var/const will not be allowed, since it will be considered a duplicate identifier.

Issue History

Date Modified Username Field Change
2018-02-03 12:02 Thaddy de Koning New Issue
2018-02-03 12:02 Thaddy de Koning Status new => assigned
2018-02-03 12:02 Thaddy de Koning Assigned To => Michael Van Canneyt
2018-02-03 12:21 Thaddy de Koning Note Added: 0106186
2018-02-03 12:23 Thaddy de Koning Note Edited: 0106186 View Revisions
2018-02-03 12:34 Thaddy de Koning Note Edited: 0106186 View Revisions
2018-02-04 10:42 Thaddy de Koning Note Added: 0106214
2018-02-24 17:34 Michael Van Canneyt Fixed in Revision => 1466.
2018-02-24 17:34 Michael Van Canneyt Note Added: 0106582
2018-02-24 17:34 Michael Van Canneyt Status assigned => resolved
2018-02-24 17:34 Michael Van Canneyt Fixed in Version => 3.1.1
2018-02-24 17:34 Michael Van Canneyt Resolution open => fixed
2018-02-24 17:34 Michael Van Canneyt Target Version => 3.2.0
2018-02-24 17:36 Michael Van Canneyt Note Edited: 0106582 View Revisions