Recent

Author Topic: Record with methods?  (Read 39682 times)

Wodzu

  • Full Member
  • ***
  • Posts: 171
Record with methods?
« on: March 13, 2010, 05:31:44 pm »
Is it supported in FPC? I am afraid not, cause my program won't compile.

What is closest to the record with methods which we have in Delphi?

idog

  • Full Member
  • ***
  • Posts: 121
    • www.idogendel.com (Hebrew)
Re: Record with methods?
« Reply #1 on: March 13, 2010, 06:11:39 pm »
I believe that would be the old Object data type:
http://community.freepascal.org:10000/docs-html/ref/refch5.html

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Record with methods?
« Reply #2 on: March 13, 2010, 06:18:33 pm »
Is it supported in FPC? I am afraid not, cause my program won't compile.

What is closest to the record with methods which we have in Delphi?
regular procedure with the first var parameter of the record.

Zaher

  • Hero Member
  • *****
  • Posts: 680
    • parmaja.org
Re: Record with methods?
« Reply #3 on: March 13, 2010, 07:05:04 pm »
record with method = object

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: Record with methods?
« Reply #4 on: March 13, 2010, 10:47:32 pm »
there seems to be a lot of confusion among newer freepascal users regarding classes and objects. a class is a data type (=the abstract thing) and the object is a variable of that type (=the "real" thing, a.k.a. an instance of that class). a class is a like a record that includes fields (=variables) and methods (=procedures and functions). among those methods there are two special ones called constructor and destructor; they might be inherited from the parent of the class. to instatiate (=create) an object one must call its constructor; when done with it, its destructor must be called.

why would anyone want to use classes/objects instead of standard procedures/functions? mainly for convenience and... elegance: instead of issuing something like a_procedure(a_record_var) one can issue an_object.a_method. and then there's inheritance, polymorphism...

try this: http://www.delphibasics.co.uk/Article.asp?Name=OO
« Last Edit: March 15, 2010, 02:38:16 pm by Troodon »
Lazarus/FPC on Linux

idog

  • Full Member
  • ***
  • Posts: 121
    • www.idogendel.com (Hebrew)
Re: Record with methods?
« Reply #5 on: March 13, 2010, 11:09:38 pm »
there seems to be a lot of confusion among newer freepascal users regarding classes and objects. a class is a data type (=the abstract thing) and the object is a variable of that type

I (and I think Zaher too) didn't mean "an object" (as in an instance of a class) but "the Object type" (as in "type TMyObj = Object ... end; "). It's probably not a recommended programming tool anymore, but it's closer to Delphi's "Record with methods" than classes, no?

Zaher

  • Hero Member
  • *****
  • Posts: 680
    • parmaja.org
Re: Record with methods?
« Reply #6 on: March 13, 2010, 11:21:18 pm »
"Record with methods" = Object (a created from class)
I think it is not a feature, it step to make managed objects, so jumping to managed objects directly it is the feature.

The problem it how to make that class have one Create method as they have on Destroy method.

Wodzu

  • Full Member
  • ***
  • Posts: 171
Re: Record with methods?
« Reply #7 on: March 13, 2010, 11:44:35 pm »
idog it seems Zaher meant an instance of class not an Object data type ;)

I was reading here somewhere post about the difference between the Object and instance of class but don't remebmer it now exactly.

[After few minutes]

Ok, I've read the link which you gave me. It is clear now, thanks :)

@Zaher I really hope that you had not on your mind a garbage collector, I really hope that   ;D

idog

  • Full Member
  • ***
  • Posts: 121
    • www.idogendel.com (Hebrew)
Re: Record with methods?
« Reply #8 on: March 14, 2010, 12:27:21 am »
@Wodzu: Well I'm just a programmer, not a telepath  :D

I can understand why FPC developers wouldn't bother with Record methods (and, BTW, I won't be surprised if the Object data type disappeared from the Pascal language altogether). Still, they are a sweet language feature...

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: Record with methods?
« Reply #9 on: March 14, 2010, 10:52:34 am »
Note that a danger of using TP objects instead of records with methods is that TP objects are not safe with automated types (ansistrings, dynamic arrays). So I wouldn't recommend this.

I would simply define normal procedures + records.

idog

  • Full Member
  • ***
  • Posts: 121
    • www.idogendel.com (Hebrew)
Re: Record with methods?
« Reply #10 on: March 14, 2010, 01:51:28 pm »
TP objects are not safe with automated types

Ugh!

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: Record with methods?
« Reply #11 on: March 14, 2010, 02:13:27 pm »
Warning: Rant! :)

Classes, TP objects, records with methods... why not complicate the hell out of our programming habits? I mean, instead of focusing on the logic of our programs we could spend twice as much time to gain, perhaps, a few microseconds in a tight loop, especially if that increases the chance that the program will not work as expected. I think the classes concept is clear enough to avoid any confusion and implementing more "exotic" stuff for the sake of compatibility with (little used features in?) Delphi only drains the development resources of this community. My 2 cents.
« Last Edit: March 14, 2010, 02:56:50 pm by Troodon »
Lazarus/FPC on Linux

Zaher

  • Hero Member
  • *****
  • Posts: 680
    • parmaja.org
Re: Record with methods?
« Reply #12 on: March 14, 2010, 02:25:51 pm »
Record not need "garbage collector" to free it, so if we have feature in FreePascal a
Record have methods and can inherit it from another record and we can override some virtual methods, and then we have Create and Destroy in standard can we override them for override the initialization of this record, (i am dreaming  ::) ), and we can inherit that record from a Class, so we will have a managed objects in free pascal. %)

Did any one understand me :)

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: Record with methods?
« Reply #13 on: March 14, 2010, 02:50:10 pm »
Well, they don't need garbage collection because they don't sit on the heap, i.e., they are permanently allocated.
Lazarus/FPC on Linux

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: Record with methods?
« Reply #14 on: March 14, 2010, 03:22:23 pm »
Record not need "garbage collector" to free it, so if we have feature in FreePascal a
Record have methods and can inherit it from another record and we can override some virtual methods, and then we have Create and Destroy in standard can we override them for override the initialization of this record, (i am dreaming  ::) ), and we can inherit that record from a Class, so we will have a managed objects in free pascal. %)

Yes, but only within the scope of one procedure. Is that really worth the trouble? :-)
 

 

TinyPortal © 2005-2018