Recent

Author Topic: Why is var after type in new Projekt?  (Read 551 times)

coradi

  • Full Member
  • ***
  • Posts: 202
Why is var after type in new Projekt?
« on: June 04, 2026, 06:31:48 pm »
Why is var after type?
Doesn't it make sense to put globals variable more top?


Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.   private
  13.  
  14.   public
  15.  
  16.   end;
  17.  
  18. var
  19.   Form1: TForm1;
  20.  
  21. implementation
  22.  
  23. {$R *.lfm}
  24.  
  25. end.
       
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

440bx

  • Hero Member
  • *****
  • Posts: 6556
Re: Why is var after type in new Projekt?
« Reply #1 on: June 04, 2026, 06:37:39 pm »
var comes after type because when you declare a var you need to know the type of the var.  You can't say
Code: Pascal  [Select][+][-]
  1. var : I_don't_know_the_type_yet_I_ll_figure_it_out_later;
and expect that to compile.  Clairevoyant compilers are not a thing yet.

HTH.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

coradi

  • Full Member
  • ***
  • Posts: 202
Re: Why is var after type in new Projekt?
« Reply #2 on: June 04, 2026, 06:53:29 pm »
ah, ok, yes, that make sense
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

creaothceann

  • Sr. Member
  • ****
  • Posts: 389
Re: Why is var after type in new Projekt?
« Reply #3 on: June 05, 2026, 02:16:30 am »
Doesn't it make sense to put globals variable more top?

Constants, types and variables can be placed in any order because they can reference each other.

For example a constant can be defined as const MyConst = SizeOf(MyType) + 10, or a variable can be defined as var MyVar : MyType = high(MyType).

cdbc

  • Hero Member
  • *****
  • Posts: 2856
    • http://www.cdbc.dk
Re: Why is var after type in new Projekt?
« Reply #4 on: June 05, 2026, 08:43:31 am »
Hi
Pascal = 'Declare before use', simple as that.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

coradi

  • Full Member
  • ***
  • Posts: 202
Re: Why is var after type in new Projekt?
« Reply #5 on: June 05, 2026, 09:17:25 am »
ist VAR x y Z not declare?
Is TYPE xyz not Declare too?
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

Thaddy

  • Hero Member
  • *****
  • Posts: 19388
  • Glad to be alive.
Re: Why is var after type in new Projekt?
« Reply #6 on: June 05, 2026, 09:33:29 am »
No, although technically both are also declarations.
Type provides the formal description also known as  declaration or definition
Var provides the implementation for what is described for the type also know as declaration or instantiation.

Therefor type needs to be before var since var implements type.
A var is an instance of a type
types are not variables.
« Last Edit: June 05, 2026, 09:51:45 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

LeP

  • Sr. Member
  • ****
  • Posts: 428
Re: Why is var after type in new Projekt?
« Reply #7 on: June 05, 2026, 10:12:15 am »
While we're on the subject, another thing that can be useful is to use a type before its definition.
This can be done if it's used in the same declaration block where the type is declared (WORKS ONLY FOR TYPES):

Code: Pascal  [Select][+][-]
  1. type
  2.   IForm1 = ^TForm1;                   //<--TForm1 is used before it's declaration, but inside the same block. THIS IS OK and compiles.
  3.  
  4.   TForm1 = class(TForm)
  5.     Button1: TButton;
  6.   private
  7.     { Private declarations }
  8.   public
  9.     { Public declarations }
  10.   end;

Code: Pascal  [Select][+][-]
  1. type
  2.   IForm1 = ^TForm1;                   //<--TForm1 is used before it's declaration, but OUTSIDE the declaration block. THIS IS NOT OK and doesn't compile !!!
  3.  
  4. type
  5.   TForm1 = class(TForm)
  6.     Button1: TButton;
  7.   private
  8.     { Private declarations }
  9.   public
  10.     { Public declarations }
  11.   end;
Un Sistema per domarli, un IDE per trovarli, un codice per ghermirli e nel framework incatenarli.
An operating system to tame them, an IDE to find them, a code to catch them and in the framework chain them.

Xenno

  • Full Member
  • ***
  • Posts: 131
    • BS Programs
Re: Why is var after type in new Projekt?
« Reply #8 on: June 05, 2026, 10:43:19 am »
It is also possible:

Code: Pascal  [Select][+][-]
  1. type
  2.   TClassA = class;
  3.   TClassB = class
  4.     FMyA: TClassA;
  5.   end;
  6.  
  7. var
  8.   NewA: TClassA;
  9.   NewB: TClassB;
  10.  
  11. type
  12.   TClassA = class
  13.     FMyB: TClassB;
  14.   end;
Lazarus 4.6, Windows 10, https://www.youtube.com/@bsprograms
If I want to share, I give. If I want money, I sell. I don't set traps.

Thaddy

  • Hero Member
  • *****
  • Posts: 19388
  • Glad to be alive.
Re: Why is var after type in new Projekt?
« Reply #9 on: June 05, 2026, 11:30:03 am »
Yes, forward type declarations are possible but the var needs to follow otherwise the compiler errors with var X is not fully defined. You can not declare a var before the type is fully defined.
objects are fine constructs. You can even initialize them with constructors.

jamie

  • Hero Member
  • *****
  • Posts: 7829
Re: Why is var after type in new Projekt?
« Reply #10 on: June 05, 2026, 12:00:56 pm »
For those that are more internal, the compiler is designed to be a single pass compiler which makes it quicker to build applications.

 But there are some optimizers that can't be taken advantage of due to this method but, in today computers that might not be such a problem unless you were targeting small under powered devices where it makes a difference to have multiple pass compilers.

  So, in short, the compiler could do a complete sweep of the unit file to build all the type references and then do the code build on the second pass, but as far as I know that does not happen and for the most part it really isn't that beneficial. The best you have now is the ability to create a forward reference for a pointer to an item because the pointer is always the same size throughout. This includes forward references to class that have not yet been typed because they are further down the list in the file.

The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 19388
  • Glad to be alive.
Re: Why is var after type in new Projekt?
« Reply #11 on: June 05, 2026, 12:46:08 pm »
Although that is a good description even multipass compilers like clang and C++ will throw an error if a var is used before its type is defined.
objects are fine constructs. You can even initialize them with constructors.

Xenno

  • Full Member
  • ***
  • Posts: 131
    • BS Programs
Re: Why is var after type in new Projekt?
« Reply #12 on: June 05, 2026, 01:53:43 pm »
Try this:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   Classes
  10.   { you can add units after this };
  11.  
  12. type
  13.   TManager = class;
  14.   TStaff = class
  15.     Mgr: TManager;
  16.     TheName: String;
  17.   end;
  18.  
  19. var
  20.   Cindy: TStaff;
  21.   John: TManager;
  22.  
  23. type
  24.   TManager = class
  25.     HisStaff: TStaff;
  26.     TheName: String;
  27.   end;
  28.  
  29. begin
  30.   John := TManager.Create;
  31.   try
  32.     John.TheName := 'Johnny B. Nice';
  33.     Cindy := TStaff.Create;
  34.     try
  35.       Cindy.TheName := 'Cindy Crawying';
  36.       Cindy.Mgr := John;
  37.       John.HisStaff := Cindy;
  38.       WriteLn('Cindy''s manager: ' + Cindy.Mgr.TheName);
  39.       WriteLn('John''s staff: ' + John.HisStaff.TheName);
  40.       ReadLn;
  41.     finally
  42.       Cindy.Free;
  43.     end;
  44.   finally
  45.     John.Free;
  46.   end;
  47. end.

The output will be:

Cindy's manager: Johnny B. Nice
John's staff: Cindy Crawying

Lazarus 4.6, Windows 10, https://www.youtube.com/@bsprograms
If I want to share, I give. If I want money, I sell. I don't set traps.

Thaddy

  • Hero Member
  • *****
  • Posts: 19388
  • Glad to be alive.
Re: Why is var after type in new Projekt?
« Reply #13 on: June 05, 2026, 02:02:03 pm »
Yes, of course, but the announcement that the compiler can rely on the type being made is the same thing:
There is a definition for type before var.
BTW your example can bite.
objects are fine constructs. You can even initialize them with constructors.

Warfley

  • Hero Member
  • *****
  • Posts: 2070
Re: Why is var after type in new Projekt?
« Reply #14 on: June 05, 2026, 02:36:17 pm »
Well the better question is: Why do you need a global variable in the first place? The answer is actually that you don't. If you don't like the variable to be there you could also remove (move it to the LPR locally if statically created) or make it a singleton or something fancy like that.

 

TinyPortal © 2005-2018