Recent

Author Topic: TApplication vs Application  (Read 384 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
TApplication vs Application
« on: July 03, 2022, 06:02:48 pm »
While I was exploring the files of a small program I typed in Lazarus, I explored a file called "project1.lpr" and found this code inside it:

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   {$IFDEF HASAMIGA}
  10.   athreads,
  11.   {$ENDIF}
  12.   Interfaces, // this includes the LCL widgetset
  13.   Forms, Unit1
  14.   { you can add units after this };
  15.  
  16. {$R *.res}
  17.  
  18. begin
  19.   RequireDerivedFormResource:=True;
  20.   Application.Scaled:=True;
  21.   Application.Initialize;
  22.   Application.CreateForm(TForm1, Form1);
  23.   Application.Run;
  24. end.
  25.  
  26.  

I noticed that the code used "Application" rather than "TApplication", I think it should use "TApplication", so why this change in the name?
La chose par la chose est rappelé.

VisualLab

  • New Member
  • *
  • Posts: 28
Re: TApplication vs Application
« Reply #1 on: July 03, 2022, 06:15:50 pm »
TApplication is a class. Application is:

- variable of type (class) TApplication,
- is global,
- created in the initialization section of the Forms unit.

MarkMLl

  • Hero Member
  • *****
  • Posts: 4728
Re: TApplication vs Application
« Reply #2 on: July 03, 2022, 06:26:32 pm »
I noticed that the code used "Application" rather than "TApplication", I think it should use "TApplication",

Why do you think that? I'm not trying to be hostile or give you the brushoff, I'd just like to work through this methodically.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
Re: TApplication vs Application
« Reply #3 on: July 03, 2022, 06:34:05 pm »
Quote
Why do you think that? I'm not trying to be hostile or give you the brushoff, I'd just like to work through this methodically

This question appropriately have to be asked for a human? soft human being? why do you think you are talking to a human?  :)

I'm joking with you  :D

Really, I saw "T" letter removed, so I thought that we have maybe two different classes.
La chose par la chose est rappelé.

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
Re: TApplication vs Application
« Reply #4 on: July 03, 2022, 06:36:01 pm »
Quote
- created in the initialization section of the Forms unit.

Do you mean a hidden section? or an internal section in "Forms" unit?
La chose par la chose est rappelé.

y.ivanov

  • Sr. Member
  • ****
  • Posts: 435
Re: TApplication vs Application
« Reply #5 on: July 03, 2022, 06:57:01 pm »
Quote
- created in the initialization section of the Forms unit.

Do you mean a hidden section? or an internal section in "Forms" unit?

https://www.freepascal.org/docs-html/current/ref/refse112.html#x233-25700016.2

Look at last 2 diagrams, initialization part and finalization part.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MarkMLl

  • Hero Member
  • *****
  • Posts: 4728
Re: TApplication vs Application
« Reply #6 on: July 03, 2022, 07:06:22 pm »
Really, I saw "T" letter removed, so I thought that we have maybe two different classes.

OK, let's try and go through this gradually: while VisualLab's answer is correct I think that I can help you catch some fish tomorrow as well :-)

A leading "T" is a convention, /not/ enforced by the language or Lazarus IDE, that indicates that you're looking at a type.

A class is a particular flavour of type, and there's a great deal of magic in the compiler which does things to classes over and above what's done to other types (integers, arrays, strings and so on).

A class is instantiated by a call to its constructor, which in Object Pascal is normally called Create(). The constructor returns an instance of that class, which includes allocated memory and so on.

A class can have memory which is accessible by all instances, which is generally indicated by field names etc. marked with the "class" attribute. You would access a property (etc.) marked with the class attribute via the type name (e.g. TApplication).

If you start off at https://lazarus-ccr.sourceforge.io/docs/lcl/ and use the index to find the description of TApplication, you see that nothing is marked with the class attribute. Therefore, all storage documented there is allocated to instances; you would access a property (etc.) viaa named instance (e.g. Application).

The above is a general discussion of types, classes and instances. Below is more specific.

The .lpr file ("Lazarus PRogram") is generated by the IDE, so it's reasonable to assume that it's correct.

The Application object is the sole instance of TApplication. The constructor is called in the Forms unit, and the global variable representing the instance is stored (and exported) there.

The IDE-generated code in the .lpr file does various things to the sole instance if TApplication:

Code: Pascal  [Select][+][-]
  1. begin
  2.   RequireDerivedFormResource:=True;
  3.   Application.Scaled:=True;
  4.   Application.Initialize;
  5. ...
  6.  

It then reads the layout of the program's main form:

Code: Pascal  [Select][+][-]
  1. ...
  2.   Application.CreateForm(TForm1, Form1);
  3. ...
  4. [code]
  5.  
  6. which among other things instantiates the class TForm1 into the variable Form1, then it initialises the properties of the (visual and non-visual) components which make up that form from the .lfm file. It may then go on to instantiate other forms, all of this is handled "automagically" by the IDE and is not stuff you are expected to write.
  7.  
  8. Finally, having completed initialisation, it transfers control to code associated with the main form.
  9.  
  10. Your program is now running.
  11.  
  12. MarkMLl
  13.  
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
Re: TApplication vs Application
« Reply #7 on: July 03, 2022, 07:28:19 pm »
Quote
OK, let's try and go through this gradually: while VisualLab's answer is correct I think that I can help you catch some fish tomorrow as well :-)

A leading "T" is a convention, /not/ enforced by the language or Lazarus IDE, that indicates that you're looking at a type.

A class is a particular flavour of type, and there's a great deal of magic in the compiler which does things to classes over and above what's done to other types (integers, arrays, strings and so on).

A class is instantiated by a call to its constructor, which in Object Pascal is normally called Create(). The constructor returns an instance of that class, which includes allocated memory and so on.

A class can have memory which is accessible by all instances, which is generally indicated by field names etc. marked with the "class" attribute. You would access a property (etc.) marked with the class attribute via the type name (e.g. TApplication).

If you start off at https://lazarus-ccr.sourceforge.io/docs/lcl/ and use the index to find the description of TApplication, you see that nothing is marked with the class attribute. Therefore, all storage documented there is allocated to instances; you would access a property (etc.) viaa named instance (e.g. Application).

The above is a general discussion of types, classes and instances. Below is more specific.

The .lpr file ("Lazarus PRogram") is generated by the IDE, so it's reasonable to assume that it's correct.

The Application object is the sole instance of TApplication. The constructor is called in the Forms unit, and the global variable representing the instance is stored (and exported) there.

Thank you for this rich explaining. I think I understand it about 70% or 80%, you know I'm a beginner so it's not expected that I'll understand everything 100%.

I thought you'll make your explaining more complex, but I'll tell you a trick rather than confusion, if you are talking to beast and you want him not understand you perfectly, so talk with many humanly concepts, but if you want him to understand you 100% talk with beast concepts like you know wild concepts, animals, nature, something like that. I'm joking  :D but thank you for your efforts.
La chose par la chose est rappelé.

VisualLab

  • New Member
  • *
  • Posts: 28
Re: TApplication vs Application
« Reply #8 on: July 04, 2022, 12:02:14 am »
The convention discussed by MarkMLl, ie starting a class name with one capital letter (such a name distinction) is not used very often in libraries. But for example the Qt library (https://doc.qt.io/, C++) uses this approach where class names (and some other types) start with the letter "Q". In the ROOT library (https://root.cern/about/license/, also C++) the capital letter "T" is also used as in Object Pascal. Also, the types of some GNOME libraries use a similar convention, where is used the letter "G" (https://docs.gtk.org/gobject/).

 

TinyPortal © 2005-2018