Recent

Author Topic: Proper way to learn FreePascal even not in a R&D(Lazarus) environment  (Read 1075 times)

PeterHu

  • New Member
  • *
  • Posts: 20
I am not new to freepascal and Lazarus,but I was confused for years regarding the language.It is kind of easy (vs C/C++) to make a form and have somethings done.But it is very different(here just to me) because there is very less information in the code generated by the IDE,esp. those libraries.For example,when interop with Sqlite3 controls,I can't see where it is been initialized,when the database is opened,and it is a bit harder to iterate records with a common List Box because(again just to me) the Sqlite3 wrapper class /library encapsulates too many deails so I am not capable to making use of it with a ListBox.

Another thing,when I tried to exercise in a pure console program,I failed to use many many of the libraies/classes,visual or none-visual.To make it clearer,I take c/c++ as an example here only (No language war please):in c/c++ when I want to do something,I #include <that_header.h> and go;but in lazarus(downloading the installation exe file and installing automatically-->assume those neccessory thing should have already in the system envirionment path under windows 10 to specify) or a text editor with freepascal,I know there is such a library to help me do something by searching the documentations,and so  I uses xxx,yyy; but failed to compile,the compiler just complains failed to found those units.but why?She should know much more than me that's just a fcl/lcl/rtl or something else lives exactly with her.

So,my questions are :
1.I want to learn FreePascal as days I learnt c,I want to know everything that I did & to do ,and I need to know what I have to include to the source file/units,what is the proper way?
2.For those beautiful libraries,how to use them in a hardcoded way,not just input some values in the IDE control property field?
3. Is there any intermediate-advanced tutorial to the very most updated FreePascal language <just the language>?

Thank you so much in advance.

Best Regards.

cdbc

  • Hero Member
  • *****
  • Posts: 1518
    • http://www.cdbc.dk
Re: Proper way to learn FreePascal even not in a R&D(Lazarus) environment
« Reply #1 on: August 03, 2024, 12:55:23 pm »
Hi
Maybe this can be of use to you: www.copperwood.com/pub/FreePascalFromSquareOne.pdf
N.b.: In pascal we use the 'uses' clause to import different units, to /ahemm/ use in our projects. You get 2 of them in each unit a 'public' and a 'private' one, e.g.:
Code: Pascal  [Select][+][-]
  1. unit clean.new;
  2. {$mode ObjFPC}{$H+}
  3. interface
  4. uses classes, sysUtils, sqlite3dyn; //<--- here's the public 'uses'
  5.  
  6. implementation
  7. uses strutils; //<--- and this would be the private 'uses'
  8.  
  9.  
  10. end.
  11.  
  12.  
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

VisualLab

  • Sr. Member
  • ****
  • Posts: 464
Re: Proper way to learn FreePascal even not in a R&D(Lazarus) environment
« Reply #2 on: August 03, 2024, 04:25:13 pm »
Another thing,when I tried to exercise in a pure console program,I failed to use many many of the libraies/classes,visual or none-visual.To make it clearer,I take c/c++ as an example here only (No language war please):in c/c++ when I want to do something,I #include <that_header.h> and go;but in lazarus(downloading the installation exe file and installing automatically-->assume those neccessory thing should have already in the system envirionment path under windows 10 to specify) or a text editor with freepascal,I know there is such a library to help me do something by searching the documentations,and so  I uses xxx,yyy; but failed to compile,the compiler just complains failed to found those units.but why?She should know much more than me that's just a fcl/lcl/rtl or something else lives exactly with her.

You would experience similar difficulties if you tried to learn languages ​​like C#, Java, or Rust. Most languages newer than C++ do not use such archaic solutions as header files, a preprocessor ("text substituter") or "overcomplicated" (but also half-baked) code builders (e.g. CMake). Probably learning the syntax of the above languages ​​would be a bit easier than Pascal and Object Pascal, because their creators initially assumed that using parts of C++ syntax was a good idea (and after years some people decided that they had made a mistake - see Gosling). As for "objectivity", C++ is in many respects quite archaic compared to Object Pascal or C# and Java (Rust is not object-oriented). This is clearly visible, for example, in the Qt library, where the missing "elements" of the language are patched in the source code by macros, and the code spit out from the preprocessor is transformed with various "magic" translators run before the C++ compiler.

C++ has definitely gone in (at least) a strange direction (hence the jokes about the language being designed by a committee, but unfortunately it's true). And now, after all these years, there's no turning back from these oddities and "programming bureaucracy."

Either way, if you want to program using FCL and LCL (GUI, etc.) you need to familiarize yourself not only with Pascal (structures, procedures) but also with Object Pascal (classes). The best solution will be a large book (it may be intended for Delphi, because it uses Object Pascal). That's what I did (many years ago). Plus YouTube videos showing various examples (maybe for Delphi too), because it's mostly about the language (and source code).

egsuh

  • Hero Member
  • *****
  • Posts: 1449
Re: Proper way to learn FreePascal even not in a R&D(Lazarus) environment
« Reply #3 on: August 03, 2024, 04:46:20 pm »
I have never used C. But your fructrations seem similar to what I felt at the beginning, especially regarding out-of-pascal language. I'll try to explain them based on my earlier experiences, but they may not be accurate. I'll just try to explain "what elses" exist out of your program itself. I do  not know command line uses. I only use Lazarus IDE.

First, you should get familiar with "components", which are a little bit different from class. Well I do not know exactly, but components look like "drop" boxes, like TMemo, TButton, etc., which you can drop from component palletts, and whose "published' properties you can edit at the object inspector (and you can access them within codes as well). There are other properties (and functions and procedures as well) which are "public", which you can access within codes (but not from object inspector).  You can see available public properties and methods from the help box, which pops up after you type like Edit1. and wait for a while.

When you drop a component from component pallets, the unit that contain that component is automatically added to the "use" clause of the unit.

Some libraries and components are not installed automatically. They are contained in "packages". Packages should have been installed. So you have to get familiar with Packages>install/uninstall menu, or online package manager.

Another issue is "requirements".  You need to see Project>Project Inspector menu. Sometimes you have to add requirements yourself(but in most cases, Lazarus issue the messages).

And View>Unit dependencies menu will be helpful.

There are some widely used library units, like Ararat Synapse, do not have packages. They only have to be "uses" clause. However, for these units to be accessed by your program, those directories must be in the search directory. You can add "search directories" in the Project>Project Options>Compiler Options > Paths.

Some units should be added to the uses clause by programmer, for example, Math, StrUtils, etc. And when a unit contains TForm, then you'll have no difficulty in using ShowMessage procedure. But within pure pascal code, you have to add Dialogs to your uses clause to use ShowMessage. There are similar things.

But at beginning, focusing on components which are on the component pallets and installing packages should be sufficuent for most cases .

For now, these are those things that you are mostly puzzled with. Go into those menus and see what they look like. As I said at the beginning, I might be wrong, but those areas were the first barriers in my case.

egsuh

  • Hero Member
  • *****
  • Posts: 1449
Re: Proper way to learn FreePascal even not in a R&D(Lazarus) environment
« Reply #4 on: August 03, 2024, 04:50:39 pm »
Quote
when interop with Sqlite3 controls,I can't see where it is been initialized,when the database is opened,and it is a bit harder to iterate records with a common List Box because(again just to me) the Sqlite3 wrapper class /library encapsulates too many deails so I am not capable to making use of it with a ListBox.

For these, you need to study database programming a little bit. On the Data Access and Data Control pages of component palletts, there are many controls for DB applications. You can do simple works only by  connecting these components (linking them), without any code. So you'd better learn usages of basic components.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11769
  • FPC developer.
Re: Proper way to learn FreePascal even not in a R&D(Lazarus) environment
« Reply #5 on: August 03, 2024, 06:50:07 pm »
So,my questions are :
1.I want to learn FreePascal as days I learnt c,I want to know everything that I did & to do ,and I need to know what I have to include to the source file/units,what is the proper way?

Study examples. If that is not enough, open an example project or make a simple drag-and-drop project, and then use the lazarus source navigation facilities to jump into the source.

Quote
2.For those beautiful libraries,how to use them in a hardcoded way,not just input some values in the IDE control property field?

Basically all the IDE does is create the components, set the properties in DFM, and in some cases link some components together (e.g. query to data source or data source to connection).

Everything can be done in pure code, but might require a event pump.

Quote
3. Is there any intermediate-advanced tutorial to the very most updated FreePascal language <just the language>?

Answered by others. Marco Cantu also has some Object Pascal document on the Delphi implementation, but I can't remember if it is free.
« Last Edit: August 05, 2024, 01:08:44 pm by marcov »

VisualLab

  • Sr. Member
  • ****
  • Posts: 464
Re: Proper way to learn FreePascal even not in a R&D(Lazarus) environment
« Reply #6 on: August 03, 2024, 07:11:30 pm »
First, you should get familiar with "components", which are a little bit different from class. Well I do not know exactly, but components look like "drop" boxes, like TMemo, TButton, etc., which you can drop from component palletts, and whose "published' properties you can edit at the object inspector (and you can access them within codes as well). There are other properties (and functions and procedures as well) which are "public", which you can access within codes (but not from object inspector).  You can see available public properties and methods from the help box, which pops up after you type like Edit1. and wait for a while.

A small explanation (correction) is needed here. Components are not some unusual (or atypical) classes. They are very much ordinary classes. Classes are organized in a hierarchical inheritance tree. One such class in this tree is TComponent. Others are TPersistent, TControl, TGraphicControl or TWinControl. Depending on which of these classes our classes (that we create) or library classes come from, they are treated differently in the IDE. In particular, in that part of it called: Object inspector.



In Object Pascal, all classes have one common ancestor: TObject. There is no such thing in C++, because it uses a multiple inheritance model. It has some advantages, but also a lot of disadvantages. On the other hand, in languages ​​such as C# and Java, there is also one common ancestor of all classes (this allows to avoid various conflicts in the implementation of classes).

In Object Pascal, classes have 4 levels of visibility: private, protected, public and published. The first three scopes work more or less like in C++. The last scope (i.e. published) exists so that certain features of classes are available from the Object Inspector. Classes in Object Pascal can have the following elements (in short):
  • private scope: fields, methods (including so-called access methods: GetXyz, SetXyz), event fields,
  • protected scope: fields, methods (including so-called access methods: GetXyz, SetXyz), events,
  • public scope: properties (read-only, read/write), methods, events,
  • published: properties only (read/write), events.
Properties are a mechanism for accessing class fields. Thanks to it, you can create read-only fields (and theoretically also write-only, but this is rather useless). The second purpose is to control data entered into the class (e.g. data verification). Properties can be of the following types:
  • simple: boolean values, characters, enumerated values, integer numbers, real numbers,
  • complex: sets, strings, arrays, records (structures), classes.
Access methods are used to support properties: reading (GetXyz) or writing (SetXYz). But they don't have to be implemented explicitly. You can also create properties that read and set fields directly. There can also be properties in a class that don't have a corresponding field, when, for example, the return value is calculated. A specific type of property is an event. An event is essentially a type of callback. Such a field in a class stores a pointer to a method present in another class. If the pointer is not nil (equivalent to nullptr in C++), then the method can be called.

The Object inspector allows you to manually modify the value of properties in specific classes. The condition for this is that the class being created must derive from at least TComponent. If we want a property of such a class to be available for editing, it should be declared in the published section. Similarly, if we want to associate an event of a given class with a method in the Object inspector (or create such a method in the code editor), the event must be declared in the published section (the event, not the field handling it!). Properties and events are displayed in separate tabs of the Object inspector because they work differently. However, class methods are not visible in the Object inspector.

If we want a complex property (e.g. containing many of its own internal properties) to be visible in the Object inspector, then such a property must be a class, additionally derived from TPersistent.

Classes used to build GUIs are derived from TControl (in some libraries called: widget). There are two types: graphic (TGraphicControl) and window (TWinControl). Window controls have: a system handle, canvas, message queue and can get focus (like e.g. TForm, TButton, TEdit, TPanel, TScrollBox). Therefore, they burden the system a bit. Graphic controls do not have their own canvas (they are drawn on the parent canvas) and cannot get focus (like e.g. TImage, TLabel, TPaintBox).

ObjectPascal classes can have virtual methods, which can be redefined in descendant classes. Access methods can also be virtual. Furthermore, methods can be overloaded (i.e., same names, but different number and types of arguments). There are no friend functions, but you can achieve something close.

ObjectPascal classes can have any number of constructors. Constructors can be virtual and can also be overloaded. There are no copy constructors. Classes can have methods: Assign and AssignTo, which are used to make deep copies of objects.

ObjectPascal classes can have only one destructor. Destructors can be virtual.

A class can have fields that are themselves classes. Then such a class is the owner of these nested classes. The owning class must create internal objects that are stored in the fields. These internal objects may be created in the class's constructor or methods. In the destructor of the owning class, all internal (nested) objects must be removed (destroyed).

Control classes used to build GUIs can be used in a graphical form editor. Objects of these classes are taken from the palette and placed on an empty window. In addition, some of the controls themselves can contain other controls placed on top of each other (layered one on top of the other). The main control (platform) is the window form (TForm). The class that is the area on which other controls are placed is called the parent. The parent-child relationship applies to the GUI and should not be confused with the owner-ownership relationship.



Each unit in Pascal and Object Pascal has a public section (keyword: interface) and a private section (keyword: implementation). In both of them, you can specify used library modules (keyword: uses). You don't have to add this word (i.e. uses) to each module, as you have to do in C and C++ with the word #include - why such terrible verbosity?).

Units can have initialization and finalization sections. They are placed at the end of the module. They are used to place instructions there that will be executed once the module is loaded (initialization) or unloaded (finalization). This of course applies to modules in compiled form. In Pascal and Object Pascal, you don't need to add something like header guards, precisely because of the modularization of the code.

It is worth mentioning the existence of generalized types and the possibility of specifying constraints in these types. This is a way of writing code that is to some extent independent of types (the equivalent of templates in C++). A specific type is specified only in code that uses such a general type. It is also possible to overload operators (to a limited extent).

And finally, the so-called type helpers. They are particularly useful for handling simple types, but they can also be used for classes. Helpers allow treating variables or constants of simple types as if they were objects. This is a kind of syntactic sugar, but it greatly simplifies the code and improves its readability.

Weiss

  • Full Member
  • ***
  • Posts: 171
Re: Proper way to learn FreePascal even not in a R&D(Lazarus) environment
« Reply #7 on: August 05, 2024, 01:53:52 am »
... The best solution will be a large book (it may be intended for Delphi, because it uses Object Pascal). That's what I did (many years ago). Plus YouTube videos showing various examples (maybe for Delphi too), because it's mostly about the language (and source code).

^^^ this is what worked for me. LARGE DELPHI BOOK. Also, might be worth installing Community Edition and following many Embarcadero tutorials, and attending their webinars. Literally, follow along and many things become clear. Those are all free as free can be. Lazarus is almost identical to Delphi, thus for learning Pascal you can ride on Delphi's back.

kapibara

  • Hero Member
  • *****
  • Posts: 624
Re: Proper way to learn FreePascal even not in a R&D(Lazarus) environment
« Reply #8 on: August 05, 2024, 02:56:48 am »
Over the years, thats what I did too. Thanks to used books at Amazon, I might now have a copy of every Delphi book ever printed. :-))

... The best solution will be a large book (it may be intended for Delphi, because it uses Object Pascal). That's what I did (many years ago). Plus YouTube videos showing various examples (maybe for Delphi too), because it's mostly about the language (and source code).

^^^ this is what worked for me. LARGE DELPHI BOOK. Also, might be worth installing Community Edition and following many Embarcadero tutorials, and attending their webinars. Literally, follow along and many things become clear. Those are all free as free can be. Lazarus is almost identical to Delphi, thus for learning Pascal you can ride on Delphi's back.
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 517
Re: Proper way to learn FreePascal even not in a R&D(Lazarus) environment
« Reply #9 on: August 05, 2024, 03:52:15 am »
Since you already know the basic ideas, my advice would be to pick a small project, fire up the Lazarus IDE and dive in.  Maybe watch a few youtube videos (they will reduce the approach avoidance issues) but then just try stuff relevant to your project.

Different people have different learning styles, so that might be the right one for you.  In any event, there is an ancient Greek aphorism that learning is like chewing stones, and I've never discovered a way to get past some of that.  A spirit of adventure helps a lot.

silvercoder70

  • New Member
  • *
  • Posts: 48
    • Tim Coates
Re: Proper way to learn FreePascal even not in a R&D(Lazarus) environment
« Reply #10 on: August 05, 2024, 05:10:31 am »
Someone earlier mentioned  Marco Cantu... he has written a text on

OBJECT PASCAL

that you can get for free. It came out in 2021. Mixture of reference and tutorial (broadly speaking) and worth getting.
P Plate on FPC | YouTube - https://www.youtube.com/@silvercoder70

PeterHu

  • New Member
  • *
  • Posts: 20
Re: Proper way to learn FreePascal even not in a R&D(Lazarus) environment
« Reply #11 on: August 05, 2024, 06:09:21 am »
Thank you all for the guideline and great help.

And also here I apologize for my ignorance about Object Pascal HandBook.I was thought this book you gentlemen mentioned about is THAT book- ten or more years ago written by the author.Searching and learnt just a moment ago that the updated version is already available since year 2021.Great!

Thank you gentlement so much again!

TRon

  • Hero Member
  • *****
  • Posts: 3254
Re: Proper way to learn FreePascal even not in a R&D(Lazarus) environment
« Reply #12 on: August 05, 2024, 06:10:24 am »
Quote
I can't see where it is been initialized,when the database is opened,and it is a bit harder to iterate records with a common List Box because(again just to me) the Sqlite3 wrapper class /library encapsulates too many deails so I am not capable to making use of it with a ListBox.
The only way to determine that is by either looking at the source-code of the component(s) or if you are lucky someone has described the process of how to do things manually in the wiki.

But, it seems to me that you are trying to make things much harder then they should be. I know you do not want to start a language war (and it is not my intention either) but does C/C++ come with a visual designer for GUI applications that can be compiled for different widgetsets ? Lazarus/LCL and C/C++ (yes or no with a GUI framework) are just two different beasts.

One starting point could be to create the controls at runtime and assigning the properties and methods manually so that you can get yourself familiar with the process. I am aware of the chicken egg problem but see below.

Also remember that in a debug session you should be able to inspect all components and their properties.

Quote
Another thing,when I tried to exercise in a pure console program,I failed to use many many of the libraries/classes,visual or none-visual.To make it clearer,I take c/c++ as an example here only (No language war please):in c/c++ when I want to do something,I #include <that_header.h> and go;but in lazarus(downloading the installation exe file and installing automatically-->assume those neccessory thing should have already in the system environment path under windows 10 to specify) or a text editor with freepascal,I know there is such a library to help me do something by searching the documentations,and so  I uses xxx,yyy; but failed to compile,the compiler just complains failed to found those units.but why?She should know much more than me that's just a fcl/lcl/rtl or something else lives exactly with her.
imho there are several points to address in there.

I often find myself also not knowing what unit to include (and too lazy to look it up in the documentation). In such cases I place the component on a form and the Lazarus IDE will automatically add the required units. This still does not work for all components/situations because often components rely on either other auxiliary units to ease the programmer or the fact that you need to use one of the ancestor or descendant classes to be able to access to achieve what you want.

Note that Free Pascal is the compiler which offers the RTL, and FCL and as such supports these units out of the box (provided that the Free Pascal compiler is configured correctly).

Lazarus and its LCL however is bound to the Lazarus IDE. As such the Free Pascal compiler does not offer an out of the box experience using LCL components. If you want to use LCL components with the Free Pascal compiler then this should be setup manually in your Free Pascal configuration.

Setting that up is a pretty daunting task and requires a lot of knowledge. That is why Lazarus offers a tool named lazbuild that can compile Lazarus projects from the command-line and which takes the burden of which component requires what exact units and their required paths out of your hands.

In fact the whole reason the Lazarus IDE exist in the first place is to take away a lot of these boring and tedious tasks that a developer should not have to burden him/herself with.

In short: don't try to write GUI applications with only the Free Pascal compiler. It really isn't worth it. There are exceptions such as when you want to program a native GUI application or use a 3th party GUI library (such as for example fpGUI).


Quote
1.I want to learn FreePascal as days I learnt c,I want to know everything that I did & to do ,and I need to know what I have to include to the source file/units,what is the proper way?
2.For those beautiful libraries,how to use them in a hardcoded way,not just input some values in the IDE control property field?
There really isn't a good answer to that question. When I want to get myself familiar with a component I just make toy projects and play with the component so I get a fair understanding of what it can do (in the process I learned what units the component relies on). Then I make a toy project that creates the component at runtime and set it up manually.

imho the only good way to learn things is by doing.

Quote
3. Is there any intermediate-advanced tutorial to the very most updated FreePascal language <just the language>?
In short (and unfortunately), no.

The reason for that is that the compiler itself is constantly updated and new features are being added and/or improved. As time goes by wiki-pages and manuals are being updated but in truth they are always lacking behind and naming those tutorials is a bit of a stretch.

Suggestion made by others are for sure a good starting point but also often lack behind or case of Delphi behaves just a little different or isn't (yet) fully supported.
« Last Edit: August 05, 2024, 06:12:20 am by TRon »
All software is open source (as long as you can read assembler)

cdbc

  • Hero Member
  • *****
  • Posts: 1518
    • http://www.cdbc.dk
Re: Proper way to learn FreePascal even not in a R&D(Lazarus) environment
« Reply #13 on: August 05, 2024, 06:21:24 am »
Hi
I'll just throw this one book in here, that is a *must* have: Julian M. Bucknall's book It's written for Delphi, but fits FreePascal perfectly  8)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

 

TinyPortal © 2005-2018