Forum > General

OOP structures in fpc and Lazarus

<< < (12/14) > >>

winni:
Hi!

Have a look in unit Types:

The very  basic structures meanwhile are advanced record:

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

Winni

Blade:

--- Quote from: jollytall on December 01, 2021, 07:22:41 pm ---Nonetheless, if you have a suggestion how to change some of the sentences, I am happy to modify.

--- End quote ---

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...
--- End quote ---


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:

--- Quote from: jollytall on December 01, 2021, 07:22:41 pm ---- 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.
--- End quote ---

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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program tgentest; {$mode objfpc}{$modeswitch advancedrecords} uses  fgl; type  TTest1 = record    a: LongInt;  end;   TTest2 = record    a: LongInt;    class operator = (const aLeft, aRight: TTest2): Boolean;  end; class operator TTest2.=(const aLeft, aRight: TTest2): Boolean;begin  Result := aLeft.a = aRight.a;end; operator = (const aLeft, aRight: TTest1): Boolean;begin  Result := aLeft.a = aRight.a;end; type  // this will complain about the missing = operator  //TTest1List = specialize TFPGList<TTest1>;  // this works however  TTest2List = specialize TFPGList<TTest2>; var  t1_1, t1_2: TTest1;  t2_1, t2_2: TTest2;begin  t1_1.a := 42;  t1_2.a := 21;  t2_1.a := 2;  t2_1.a := 4;  // the comparisons will work for both types  if t1_1 = t1_2 then    Writeln('Foo');  if t2_1 = t2_1 then    Writeln('Bar');end. 
Also Delphi code is very likely to use advanced records, because TP-style objects are considered deprecated there.

MarkMLl:

--- Quote from: PascalDragon on December 02, 2021, 09:15:57 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.

--- End quote ---

Nice one :-)

MarkMLl

howardpc:
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.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version