Recent

Author Topic: semantic delirium  (Read 5125 times)

sam707

  • Guest
semantic delirium
« on: September 21, 2013, 11:27:26 pm »
Code: [Select]
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms;

type

  { TForm1 }

  TForm1 = class(TForm)
  private
    { private declarations }
  public
    { public declarations }
    EmuRomVer: Byte; static;
    Class var RefCount: Integer;
  end;

var
  Form1: TForm1;

implementation

in that code working, can anyone tell me what is the difference between a "Class var" variable and a "static" one ? is this a free pascal compiler equivalence ? in such case the compiler could be optimized by removing one of the syntaxes

TY, cheers
« Last Edit: September 21, 2013, 11:29:57 pm by sam707 »

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1269
Re: semantic delirium
« Reply #1 on: September 22, 2013, 01:28:39 am »
Nah, they are different.

Static is persistent within instantiated classes (ie objects)

Class vars can be referenced without instantiating the class.
i := Tform1.refcount is perfectly valid.

Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

sam707

  • Guest
Re: semantic delirium
« Reply #2 on: September 22, 2013, 01:45:29 am »
bad luck lol , test that code quickly and you'll see it works.

Code: [Select]
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms;

type

  { TForm1 }

  TForm1 = class(TForm)
  private
    { private declarations }
  public
    { public declarations }
    EmuRomVer: Integer; static;
    Class var RefCount: Integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

initialization

  TForm1.EmuRomVer:=2;
  TForm1.RefCount:=1;
end.

so , my 2 variables have exactly same storage way and i can't catch any difference while accessing both of them before any object creation or allocation that you call instance.

i guess its a free pascal compiler redundance
« Last Edit: September 22, 2013, 01:47:49 am by sam707 »

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1269
Re: semantic delirium
« Reply #3 on: September 22, 2013, 04:43:24 am »
Interesting.

Code: [Select]
program Project1;

// Either ON or OFF, produces
// project1.lpr(3,2) Warning: Illegal compiler directive "$STATIC"

{$STATIC ON}

Type
  TTest = Class
  Public
    FStatic : Integer; Static;
    Class Var FClass : Integer;
  end;

Var
  a, b : TTest;
begin
  a.FStatic := 1;
  a.FClass := 2;

  WriteLn(a.FStatic); // writes 1
  WriteLn(a.FClass);  // writes 2

  WriteLn(b.FStatic); // writes 1
  WriteLn(b.FClass);  // writes 2

  WriteLn(TTest.FStatic); // writes 1
  WriteLn(TTest.FClass);  // writes 2

  TTest.FStatic:= 3;
  TTest.FClass:= 4;

  WriteLn(a.FStatic); // writes 3
  WriteLn(a.FClass);  // writes 4

  WriteLn(b.FStatic); // writes 3
  WriteLn(b.FClass);  // writes 4

  WriteLn(TTest.FStatic); // writes 3
  WriteLn(TTest.FClass);  // writes 4

  a := TTest.Create;

  a.FStatic := 1;
  a.FClass := 2;

  WriteLn(a.FStatic); // writes 1
  WriteLn(a.FClass);  // writes 2

  WriteLn(b.FStatic); // writes 1
  WriteLn(b.FClass);  // writes 2

  WriteLn(TTest.FStatic); // writes 1
  WriteLn(TTest.FClass);  // writes 2

  a.free;
end.

Seems you're right.  Not the way I thought Static behaved.   I expected errors to be raised in all the .FStatic code used before the .create...

I'm a bit worried about that Compiler Warning though.   Seems at odds with http://www.freepascal.org/docs-html/ref/refse27.html, though this document does confirm that the static behaviour seen in my code is correct (which is a long way of saying the documentation confirms I was wrong).

I'm also out of my depth, no idea if we're seeing a bug or a duplication.  Passing this one to my betters...

« Last Edit: September 22, 2013, 04:47:24 am by Mike.Cornflake »
Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

sam707

  • Guest
Re: semantic delirium
« Reply #4 on: September 22, 2013, 05:17:27 am »
TY Sir , that's what I was guessing :) nothing is perfect :

I did also hope that FPC did show compile time errors or warnings. I thought that "static" was for gobal variables and "class var" for encapsulated ones. As you see, it is not the case... found another "buglike" thing :

if you move the static var line declaration "after" the "Class var" line inside the body of the declared class , then the compiler raises errors... strange yup !

Code: [Select]
  public
    { public declarations }
    Class var RefCount: Integer;
    EmuRomVer: Byte; static;

<<< error

Code: [Select]
  public
    { public declarations }
    EmuRomVer: Byte; static;
    Class var RefCount: Integer;

<<< no error

maybe you need to do like :

Code: [Select]
  public
    { public declarations }
    Class var RefCount: Integer;
  var
    EmuRomVer: Byte; static;

and I didn't check it so far. assuming then that "Class var" is a declaration directive to begin a block (with no end ??)

anyway , all this isn't very pascalish , it is C.. ish then
« Last Edit: September 22, 2013, 05:28:32 am by sam707 »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: semantic delirium
« Reply #5 on: September 22, 2013, 09:04:41 am »
This is more appropriate to be discussed in the mailing list, let the compiler guys say about it.

 

TinyPortal © 2005-2018