Recent

Author Topic: OOP structures in fpc and Lazarus  (Read 23819 times)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: OOP structures in fpc and Lazarus
« Reply #45 on: November 30, 2021, 01:20:36 pm »
Classes:
Code: Pascal  [Select][+][-]
  1. TpostTurboPascalClass = class(TObject)...end;
confusingly all descend from "TObject" but this is not a TurboPascal object, but a more complex and more capable construct. The naming was defined by Borland, not by FPC which uses it only to be compatible. However it is a source of confusion for newcomers.

To add to the confusion: there exists a TObject that is a TP-style object in the Objects unit as that is the base type for Turbo-/FreeVision. :-\

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: OOP structures in fpc and Lazarus
« Reply #46 on: November 30, 2021, 01:29:26 pm »
PS. Isn't type SET also a primitive object datatype, kind of list-object as lack of better description.  :-\

Set is one of the fundamental types which Wirth put into the language at its inception, and (simplifying somewhat) comprises the individual bits in a register or memory location. It shouldn't be considered as a list, since it is by definition unordered.

My simplification was that the limit on a set is generally implementation-defined but somewhat larger than the number of bits in a word. 256 was typical until comparatively recently, i.e. enough to represent the ASCII character set.

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

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: OOP structures in fpc and Lazarus
« Reply #47 on: November 30, 2021, 02:40:19 pm »
PS. Isn't type SET also a primitive object datatype, kind of list-object as lack of better description.  :-\
Yes. Sets in pascal are, unlike in other languages like Python, Swift or C++, not general sets, but Bitset, i.e. their size is known at compile time and their access is always in constant time (with bit operations). Originally they where introduced into pascal to have a more highlevel concept representing the bit-flags typically used by C programs.
For example, where a C program would get an integer where you OR your flags together as an argument, Pascal provides a set, where you can do set operations (like union and inclusivity checks) on. Technically the same things happen, you can also cast sets into bit words (like integers), it's just a more high level construct. Originally pascal also did not have bit operations, as it was intended that everything should be done with sets
But due to the compatability with bit words, they are restricted only to datatypes directly representable as Integers (theoretically every data can be represented as an integer but thats not considered)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: OOP structures in fpc and Lazarus
« Reply #48 on: November 30, 2021, 04:13:14 pm »
Technically the same things happen, you can also cast sets into bit words (like integers), it's just a more high level construct.

Note that it is not, as it is a kind of like a higher level intrinsic. If the CPU supports bit level addressing or explicit bit set instructions, it can use these instead of and and orring.

Of course some of the more advanced compilers have ways of detecting a clean AND or OR to a value, and convert it. But the fact that many embedded C compilers (Keil, Microchip) have extensions for bit level access seems to indicate it is not that simple.

jollytall

  • Sr. Member
  • ****
  • Posts: 303
Re: OOP structures in fpc and Lazarus
« Reply #49 on: November 30, 2021, 06:09:38 pm »
For the angry answer
I don't know if you mean me had an angry answer, but if that is what you feel, you feel it wrong. There was nothing angry in it, (hence the 'Please' and the mentioning that I had the same confusion).

PS. Isn't type SET also a primitive object datatype, kind of list-object as lack of better description.  :-\
Sets are covered by the experts in the last few posts in details, so I cannot add much to it. I however mentioned it in Chapter 2.2 as a structure (like e.g. arrays as well) that I do not deal with, as for OOP they have no relevance.

ArtLogi

  • Full Member
  • ***
  • Posts: 184
Re: OOP structures in fpc and Lazarus
« Reply #50 on: December 01, 2021, 12:25:05 pm »
For the angry answer
I don't know if you mean me had an angry answer, but if that is what you feel, you feel it wrong. There was nothing angry in it, (hence the 'Please' and the mentioning that I had the same confusion).
Sorry for lack of clarity in my post.

No I meant _other_ responders who were irked by this statement of TObject.  :)
PS. Isn't type SET also a primitive object datatype, kind of list-object as lack of better description.  :-\
Sets are covered by the experts in the last few posts in details, so I cannot add much to it. I however mentioned it in Chapter 2.2 as a structure (like e.g. arrays as well) that I do not deal with, as for OOP they have no relevance.
Yes, answers were nice. I also need to re-read your really nice summary article again.


edit. For clarity I move this following "braindump" at the end here:

Quote
(even though, I do not see much difference - as of defining by type and using such [in build] handlers to access them - compared records, looking from again from surface. As in the end any structure is just series of preset and latching bits in oscillator screen if we dive deep enough). I do not want to derail this topic by trying to figure out what SET is or is not.

I will do end discussion of SET and a lists from my part to this quote from Merriam-Webster dictionary, as non-native English user: "List: a simple series of words or numerals (such as the names of persons or objects)"
« Last Edit: December 01, 2021, 01:10:10 pm by ArtLogi »
While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: OOP structures in fpc and Lazarus
« Reply #51 on: December 01, 2021, 01:21:04 pm »
No I meant _other_ responders who were irked by this statement of TObject.  :)

I'm sorry, that was probably me.

I agree, the choice of names is irksome :-)

Quote
I will do end discussion of SET and a lists from my part to this quote from Merriam-Webster dictionary, as non-native English user: "List: a simple series of words or numerals (such as the names of persons or objects)"

Yes, but note that while there is no explicit statement as to whether or not that's ordered the word "series" implies it is.

The important point is that sets are unordered, and set elements are uncounted. So adding an element to a list twice has exactly the same result as adding it once, and once you've removed an element removing it a second time has no effect.

(That reads like the start of Laws of Form, which might be familiar to some.)

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

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: OOP structures in fpc and Lazarus
« Reply #52 on: December 01, 2021, 02:47:02 pm »
I will do end discussion of SET and a lists from my part to this quote from Merriam-Webster dictionary, as non-native English user: "List: a simple series of words or numerals (such as the names of persons or objects)"

Well that is the common understanding of a list, but we are talking about programming here, and as such have our own terminology (the same way that the dictionary definition of a tree says something about wood, while for computer scientists a tree is a datastructure).
There are professional glossaries, for example wikipedia has a neat one for CS terminology: https://www.wikiwand.com/en/Glossary_of_computer_science
List:
Quote
An abstract data type that represents a countable number of ordered values, where the same value may occur more than once. An instance of a list is a computer representation of the mathematical concept of a finite sequence; the (potentially) infinite analog of a list is a stream.[139]: §3.5  Lists are a basic example of containers, as they contain other values. If the same value occurs multiple times, each occurrence is considered a distinct item.
Set:
Quote
Is an abstract data type that can store unique values, without any particular order. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.

Language is not really universal, each profession has it's own terminology and means potentially completely different things. A big part of learning a profession is learning the terminology to efficiently communicate
« Last Edit: December 01, 2021, 02:48:41 pm by Warfley »

Blade

  • Full Member
  • ***
  • Posts: 177
Re: OOP structures in fpc and Lazarus
« Reply #53 on: December 01, 2021, 05:35:08 pm »
advanced records...
...provide feedback.
...any comment you have or any topic that should be added...

"In the world of Free Pascal, one can question what to use advanced records at all, when object is still
fully supported and class is also available. Probably it is again not a top priority to use...
...Advanced records, or also called extended records are a rarely used feature of Pascal."

Outside of correcting the grammar, the document seems to give some mixed signals about advanced records.  I don't think that was necessarily the intention, as it's conveyed that OOP is a tool to use when appropriate. Maybe the introduction language used about advanced records should be neutral, where they are presented as being of equal standing to OOP's objects and classes.  Kind of like, "another tool in the toolbox".

Later in the document, how advance records compare to other structures are given, which is good information.  I'm just emphasizing the introduction, as a newbie/beginner might get a negative impression, as it's something not needed.  However, many users of Free Pascal can be hobbyists, IT personnel, workers or owners of a small business creating utility programs for productivity, etc...  In the context of the task or smaller size of the programs they are creating, records or advanced records could be highly suitable.

jollytall

  • Sr. Member
  • ****
  • Posts: 303
Re: OOP structures in fpc and Lazarus
« Reply #54 on: December 01, 2021, 07:22:41 pm »
Well, if it gives mixed signals, it might be because I have mixed feelings. My approach is the following:
- Advanced records are really a rarely used feature in the Free Pascal world. I do not have statistics, but it would be interesting to see how many one can find vs. e.g. class.
- Experts clearly stated above in the discussion, that it is there in FPC for full Delphi compatibility and also the reason is given why Embarcadero created it at the first place.
- If there is a tool and that is the best for a given purpose, we should not be shy to use it. This is why I detailed it.
- However there are not too many real usecases where advanced records have a real benefit. To me the only one is the variable record with methods, although I have never come across such a case in real life.
- As it is clear from my document, all these structures behave slightly differently in different cases, and for the hobbyists and alike, that would be an extra reason not to use advanced records. Somebody who is not using these features every day, it is much easier to remember only one-two structures. My recommendation after studying them for some time, if somebody starts programming and does not have a very strong argument to use something else, stick to only two structures, record for very simple cases and class for everything else where OO features are needed.

Last my trump card: I like if my programs are clean and simple and are based on the standard of the tool I use. So, I try to stick to mode objfpc and advanced records are simply not there (except if its modeswitch is used).

Nonetheless, if you have a suggestion how to change some of the sentences, I am happy to modify.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: OOP structures in fpc and Lazarus
« Reply #55 on: December 01, 2021, 07:45:35 pm »
Hi!

Have a look in unit Types:

The very  basic structures meanwhile are advanced record:

TSize, Rect, RectF, Point, PointF ....

Winni

Blade

  • Full Member
  • ***
  • Posts: 177
Re: OOP structures in fpc and Lazarus
« Reply #56 on: December 02, 2021, 12:54:25 am »
Nonetheless, if you have a suggestion how to change some of the sentences, I am happy to modify.

I think there are 3 elements to this:

1) Opinion and sentiment

This is kind of tricky, thus I gave you a bit of gentle push back.  I don't think it's my place to directly interfere or edit what an author wants to convey to the audience.  It is arguably better to ask you to elaborate about a point, to have a greater understanding about your views, and then for you decide how you want the information presented.  Your experience with using Object Pascal is totally valid.  It's great that we can discuss our different views, and maybe find common ground or learn something new.

2) Factual data

You are doing a good job on this.  Clearly, you have done your homework.  Where you are unsure about something, you are asking questions.  I think it's best for the more advanced users and developers (as is happening) to make those corrections as they see fit to.

3) Grammar and spelling

I can be of some help on this aspect.  Once there is a more mature version of the document, then people such as myself can make corrections for greater clarity.  In order to get a finished document, it's not anything to worry too much about for now, and it's arguably better not to interfere with a person's writing process.

Quote
I like if my programs are clean and simple...


Totally agree.  I always try to go the simple route first, before adding complexity.  But, we must admit that there are many views and directions that a programmer can have or choose.  This is why I like Object Pascal, because we have great options that can be used as necessary.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: OOP structures in fpc and Lazarus
« Reply #57 on: December 02, 2021, 09:15:57 am »
- However there are not too many real usecases where advanced records have a real benefit. To me the only one is the variable record with methods, although I have never come across such a case in real life.

Advanced records compared to classes or objects can have operator overloads that are part of the record itself. This allows the operators to be used inside generics which otherwise need to know the operators while the generic itself is declared.

For example:

Code: Pascal  [Select][+][-]
  1. program tgentest;
  2.  
  3. {$mode objfpc}
  4. {$modeswitch advancedrecords}
  5.  
  6. uses
  7.   fgl;
  8.  
  9. type
  10.   TTest1 = record
  11.     a: LongInt;
  12.   end;
  13.  
  14.   TTest2 = record
  15.     a: LongInt;
  16.     class operator = (const aLeft, aRight: TTest2): Boolean;
  17.   end;
  18.  
  19. class operator TTest2.=(const aLeft, aRight: TTest2): Boolean;
  20. begin
  21.   Result := aLeft.a = aRight.a;
  22. end;
  23.  
  24. operator = (const aLeft, aRight: TTest1): Boolean;
  25. begin
  26.   Result := aLeft.a = aRight.a;
  27. end;
  28.  
  29. type
  30.   // this will complain about the missing = operator
  31.   //TTest1List = specialize TFPGList<TTest1>;
  32.   // this works however
  33.   TTest2List = specialize TFPGList<TTest2>;
  34.  
  35. var
  36.   t1_1, t1_2: TTest1;
  37.   t2_1, t2_2: TTest2;
  38. begin
  39.   t1_1.a := 42;
  40.   t1_2.a := 21;
  41.   t2_1.a := 2;
  42.   t2_1.a := 4;
  43.   // the comparisons will work for both types
  44.   if t1_1 = t1_2 then
  45.     Writeln('Foo');
  46.   if t2_1 = t2_1 then
  47.     Writeln('Bar');
  48. end.
  49.  

Also Delphi code is very likely to use advanced records, because TP-style objects are considered deprecated there.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: OOP structures in fpc and Lazarus
« Reply #58 on: December 02, 2021, 09:41:59 am »
Advanced records compared to classes or objects can have operator overloads that are part of the record itself. This allows the operators to be used inside generics which otherwise need to know the operators while the generic itself is declared.

Nice one :-)

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

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: OOP structures in fpc and Lazarus
« Reply #59 on: December 02, 2021, 10:14:03 am »
A further advantage of advanced records in some situations is their built-in support for methods associated with variant (union-style) record parts. Classes do not allow direct declaration of any variant part.


Of course you can have a variant record field in a class instance if need be, but this is then a bolt-on and not as lightweight as directly declared in an advanced record, and it also requires an additional name layer in order to access the variant's fields, complicating the syntax.
Variant records have their limitations (you can't include fields of any managed types), but also allow an old-school versatility that is an advantage in some scenarios.

 

TinyPortal © 2005-2018