Recent

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

idog

  • Full Member
  • ***
  • Posts: 121
    • www.idogendel.com (Hebrew)
Re: Record with methods?
« Reply #15 on: March 14, 2010, 03:31:19 pm »
Classes, TP objects, records with methods... why not complicate the hell out of our programming habits? [...] I think the classes concept is clear enough to avoid any confusion and implementing more "exotic" stuff for the sake of compatibility

IMO, the advantage of Record methods has nothing to do with Delphi compatibility per se or with program performance - it's simply an intuitive, clear and easy way to program. Same reasoning as why I prefer, say, Pascal syntax to C++ syntax  :)

[Edit] I must add that my experience with this was rather limited, because I quickly found out that in my Turbo Delphi 2006 Explorer, using record methods on records that are function results caused a nasty compiler error due to a bug!
« Last Edit: March 14, 2010, 03:33:41 pm by idog »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9754
  • Debugger - SynEdit - and more
    • wiki
Re: Record with methods?
« Reply #16 on: March 14, 2010, 04:24:02 pm »

type
  TRecWithMetods = object
    FValue: Integer;
    procedure Foo;
  end;

Behaves exactly like a record. You do *NOT* need to care about freeing any memory. You do neither need to create it, nor destroy it.

And what does "but only within the scope of one procedure" mean?
The above can be used as a function result or var-parameter too.

It can be a field in a class, or in *another* object.

Of course like a record, they can not be circular. A record can not contain a field of it's own type. neither can an "object".


Quote
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.
Is that limitation documented? Never knew about it.


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: Record with methods?
« Reply #17 on: March 14, 2010, 10:15:36 pm »

Quote
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.
Is that limitation documented? Never knew about it.


Neither did I. I ran on stackoverflow, and did execute the test I found there with FPC. Bug compatible.

Jonas did own tests, and objects that are dynamically created (new(pobject) ) are safe according to him. (and indeed the test I found used objects statically)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9754
  • Debugger - SynEdit - and more
    • wiki
Re: Record with methods?
« Reply #18 on: March 14, 2010, 10:35:35 pm »
Neither did I. I ran on stackoverflow, and did execute the test I found there with FPC. Bug compatible.

Jonas did own tests, and objects that are dynamically created (new(pobject) ) are safe according to him. (and indeed the test I found used objects statically)

Is there a bug report? or did Jonas say it wouldn't be fixed? I had another bug with objects, and that got fixed => so apparently objects seem to be still supported?

Zaher

  • Hero Member
  • *****
  • Posts: 679
    • parmaja.org
Re: Record with methods?
« Reply #19 on: March 14, 2010, 10:55:00 pm »
Quote from: idog
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?

In fact i meant the both Object and Classes the both way give us the needs from "Record with methods", so no need to add this feature for me, i am dreaming about managed objects.
« Last Edit: March 14, 2010, 11:16:03 pm by Zaher »

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: Record with methods?
« Reply #20 on: March 15, 2010, 12:45:12 am »
Man, let's open up two new forum sections, one on algorithms and another one on data structures. Maybe that will keep us from fretting the small things. But that would be more related to FreePascal than to Lazarus, I guess...
« Last Edit: March 15, 2010, 12:46:58 am by Troodon »
Lazarus/FPC on Linux

idog

  • Full Member
  • ***
  • Posts: 121
    • www.idogendel.com (Hebrew)
Re: Record with methods?
« Reply #21 on: March 15, 2010, 01:01:22 am »
Man, let's open up two new forum sections, one on algorithms and another one on data structures

http://en.wikipedia.org/wiki/Algorithms_%2B_Data_Structures_%3D_Programs
Sorry, couldn't resist  :)

In fact i meant the both Object and Classes the both way give us the needs from "Record with methods", so no need to add this feature for me, i am dreaming about managed objects.

I'm not even sure what managed objects are (is it related to databases? I'm not very familiar with database programming)... anyway, as I said somewhere here, Record methods are simply a convenience, and they don't have the overhead of classes. Objects would do the trick if they handled automated types. Now I'm curious to see what will happen, regarding Martin_fr's post.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: Record with methods?
« Reply #22 on: March 15, 2010, 08:42:19 am »
Neither did I. I ran on stackoverflow, and did execute the test I found there with FPC. Bug compatible.

 Jonas did own tests, and objects that are dynamically created (new(pobject) ) are safe according to him. (and indeed the test I found used objects statically)

Is there a bug report? or did Jonas say it wouldn't be fixed? I had another bug with objects, and that got fixed => so apparently objects seem to be still supported?

The above discussion was part of an indirectly related bugreport: http://bugs.freepascal.org/view.php?id=6545

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Record with methods?
« Reply #23 on: March 15, 2010, 09:27:33 am »

type
  TRecWithMetods = object
    FValue: Integer;
    procedure Foo;
  end;

Behaves exactly like a record. You do *NOT* need to care about freeing any memory. You do neither need to create it, nor destroy it.

And what does "but only within the scope of one procedure" mean?
The above can be used as a function result or var-parameter too.

It can be a field in a class, or in *another* object.

Of course like a record, they can not be circular. A record can not contain a field of it's own type. neither can an "object".

You're proposing the C++ object model, aren't you? I don't recommend it. C++ object model is horrible (actually it mix two different object models) and is one of the things I promised myself I will never use C++ again in my life. IMHO the object model used by FPC is one of the best ones (a similar one is used by Objective C, Java, etc.). I can't see true benefits for use that model except less typing.

My vote is No.

[edit]
In the last months there are a lot of proposals to adopt C++ characteristics in FPC (i. e. "generics"). I really don't like it. Object Pascal is a great language as it is. I think it doesn't need more.
« Last Edit: March 22, 2018, 11:13:28 am by Ñuño_Martínez »
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: Record with methods?
« Reply #24 on: March 15, 2010, 11:57:10 am »
And what does "but only within the scope of one procedure" mean?
The above can be used as a function result or var-parameter too.

But such use cannot contain any dynamic types. Pointers or ansistrings/dyn arrays.   The jury is still out if this is a feature or bug (see other parts in this thread), but Delphi does the same apparantly.

Note that virtual methods are useless, since you can't use polymorphism without explicite and runtime instantiation.

So it is just a record with methods in its namespace for all practical purposes. For traditional programming the difference between x.method and method(x) is not that big. It can be that it only gets important for systems that postpone typechecks (like for..in generics, anonymous methods etc), to reduce the search scope using duck-typing, which I suspect Delphi added the record-with-methods  for.

 

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9754
  • Debugger - SynEdit - and more
    • wiki
Re: Record with methods?
« Reply #25 on: March 15, 2010, 12:12:16 pm »
And what does "but only within the scope of one procedure" mean?
The above can be used as a function result or var-parameter too.
But such use cannot contain any dynamic types. Pointers or ansistrings/dyn arrays.   The jury is still out if this is a feature or bug (see other parts in this thread), but Delphi does the same apparantly.
We are not talking a class/instance replacement => the initial question was records with methods.

The string/dyn-array issue only exists in objects that inherit from another object ("type = object(TSomeParentObject) end").

Records do not have inheritance.
So it is to the best of my knowledge safe to use "type = object" as a record-with-methods.
« Last Edit: March 15, 2010, 05:27:04 pm by skalogryz »

Zaher

  • Hero Member
  • *****
  • Posts: 679
    • parmaja.org
Re: Record with methods?
« Reply #26 on: March 15, 2010, 02:40:37 pm »
Quote from: Martin_fr
Records do not have inheritance.
So it is to the best of my knowledge safe to use "type = object" as a record-with-methods.
I agree

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Record with methods?
« Reply #27 on: March 15, 2010, 05:28:16 pm »
Records do not have inheritance.
So it is to the best of my knowledge safe to use "type = object" as a record-with-methods.

they do have an explicit inheritance;
Code: [Select]
type
  TParent = record
    a : integer;
  end;

  TChildA = record
    p : TParent;
    b : integer;
  end;

  TChildB = record
    p : TParent;
    c : Integer;
  end;
...
procedure ChangeParent(var p : TParent);
...
var
  c1: TChildA;
  c2: TChildB;

ChangeParent(c1.p);
ChangeParent(c2.p);


Paul_

  • Full Member
  • ***
  • Posts: 143
Re: Record with methods?
« Reply #28 on: March 22, 2018, 12:06:39 am »
Methods in records are only way of coding or it have different behavior in compiler? (access, memory, performance)

I made small test and can't find any measurable difference (it's like 0-2% in favor of CarList_normal_DoSomething).

Code: Pascal  [Select][+][-]
  1. {$Mode Delphi}
  2.  
  3. program record_method;
  4.  
  5. Uses sysutils, crt, EpikTimer;
  6.  
  7. Type
  8.   TCar = record
  9.     value : Integer;
  10.   end;
  11.  
  12.   TCarList_method = record
  13.     car : Array [0..999] of TCar;
  14.  
  15.     procedure DoSomething;
  16.   end;
  17.  
  18.   TCarList_normal = record
  19.     car : Array [0..999] of TCar;
  20.   end;
  21.  
  22. Const
  23.   Items = 99999;
  24.  
  25. Var
  26.   CarList_method : array [0..Items] of TCarList_method;
  27.   CarList_normal : array [0..Items] of TCarList_normal;
  28.  
  29.   time_method, time_normal : Double;
  30.  
  31.   ET : TEpikTimer;
  32.  
  33.   test_result_normal, test_result_method : Double;
  34.  
  35. // Method
  36. procedure TCarList_method.DoSomething;
  37. var
  38.   i : Integer;
  39. begin
  40.   for i := 0 to high(car) do
  41.     car[i].value := car[i].value + 1000;
  42. end;
  43.  
  44. // Normal
  45. procedure CarList_normal_DoSomething( ID : Integer );
  46. var
  47.   i : Integer;
  48. begin
  49.   With CarList_normal[ID] do
  50.     for i := 0 to high(car) do
  51.       car[i].value := car[i].value + 1000;
  52. end;
  53.  
  54. Procedure ET_Start;
  55. begin
  56.   ET.Clear; ET.Start;
  57. end;
  58.  
  59. function  ET_End() : Double;
  60. begin
  61.   ET.Stop;
  62.   Result := ET.Elapsed;
  63. end;
  64.  
  65. Procedure Test;
  66. Var
  67.   i : Integer;
  68. begin
  69.   ET_Start;
  70.   for i := 0 to Items do
  71.     CarList_method[i].DoSomething;
  72.   time_method := ET_End;
  73.  
  74.   ET_Start;
  75.   for i := 0 to Items do
  76.     CarList_normal_DoSomething(i);
  77.   time_normal := ET_End;
  78.  
  79.   test_result_method := test_result_method + time_method;
  80.   test_result_normal := test_result_normal + time_normal;
  81.  
  82.   Writeln( 'Method: ' + FloatToStr( time_method * 1000 ) +' ms');
  83.   Writeln( 'Normal: ' + FloatToStr( time_normal * 1000 ) +' ms');
  84. end;
  85.  
  86. var
  87.   i : Integer;
  88. begin
  89.   ET := TEpikTimer.Create(nil);
  90.  
  91.   for i := 0 to 99 do
  92.     Test;
  93.  
  94.   Writeln( 'Method total: ' + FloatToStr( test_result_method * 1000 ) +' ms');
  95.   Writeln( 'Normal total: ' + FloatToStr( test_result_normal * 1000 ) +' ms');
  96.  
  97.   writeln( 'Press any key..' );
  98.   readkey;
  99. end.
« Last Edit: March 22, 2018, 12:19:13 am by Paul_ »

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Record with methods?
« Reply #29 on: March 22, 2018, 12:54:11 am »
Old Post!

 However, lets not forget about the OBJECT model, which is still in use!
 
 Type
   TMyObjectType = Object
 
     xxxx
  End;


 I still very much use it. ;)


The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018