Recent

Author Topic: default method for objects  (Read 3025 times)

speter

  • Sr. Member
  • ****
  • Posts: 349
default method for objects
« on: January 14, 2022, 01:52:50 am »
G'Day Folks,

Is it possible to have a default method or similar for objects (not classes). For example:
Code: Pascal  [Select][+][-]
  1. type
  2.   tfoo = object
  3.     v : byte;
  4.     procedure init;
  5.     function getit : byte;
  6.   end;
  7. procedure tfoo.init;
  8. begin
  9.   v := 255;
  10. end;
  11. function tfoo.getit : byte;
  12. begin
  13.   result := v;
  14. end;
  15.  
  16. var
  17.   zz : tfoo;
  18.   k : byte;
  19. begin
  20.   zz.init;
  21.   k := zz.v;      // this works
  22.   k := zz.getit;  // this works
  23.   k := zz;        // i'd like to do this - have it return 'v' or call getit()
  24. end;

In other words, can I code it so that function getit is called as a default action. ;)

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: default method for objects
« Reply #1 on: January 14, 2022, 02:02:12 am »
You need to use an operator overload but  you can not do that with OBJECTS, you need to use the ADVANCEDRECORDS mode which gives you everything you are using objects for atm.

 You can define an operator := for the record.

 fpc also supports this outside too but its a little tricky.
The only true wisdom is knowing you know nothing

speter

  • Sr. Member
  • ****
  • Posts: 349
Re: default method for objects
« Reply #2 on: January 14, 2022, 03:58:52 am »
Thanks for the quick answer, jamie.

I tried:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. {$modeswitch advancedrecords}
  3. ...
  4. type
  5.   tfoo = record
  6.     v : byte;
  7.  
  8.     procedure init;
  9.     function getit : byte;
  10.     class operator Implicit(a : byte) : tfoo;
  11.   end;

But received the error msg : "Error, it is not possible to overload this operator" (pointing to line 10 above).

So I'm still missing something.  :o

Also, if I understand the implicit operator, it is like:
Code: Pascal  [Select][+][-]
  1. zz := 22;
rather than
Code: Pascal  [Select][+][-]
  1. k := zz;

cheers
S. :)
« Last Edit: January 14, 2022, 04:01:15 am by speter »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: default method for objects
« Reply #3 on: January 14, 2022, 07:23:34 am »
You use the wrong syntax for the assignment operator. You use delphi syntax but you specify mode objfpc.
Here a sketch how it would work with the correct objfpc syntax:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. {$modeswitch advancedrecords}
  3. type
  4.   Tfoo = record
  5.     v : byte;
  6.     class operator initialize(var foo:Tfoo);
  7.     class operator :=(a : byte) : tfoo;
  8.     class operator :=(a : TFoo) : Byte;
  9.   end;
  10.  
  11.   class operator Tfoo.:=(a:byte):Tfoo;
  12.   begin
  13.     Result.v := a;
  14.   end;
  15.  
  16.   class operator Tfoo.:=(a:TFoo):Byte;
  17.   begin
  18.     Result := a.v;
  19.   end;
  20.  
  21.   class operator TFoo.Initialize(var Foo:Tfoo);
  22.   begin
  23.     Foo := Default(TFoo);
  24.     Foo.v :=255;
  25.   end;
  26.  
  27. var
  28.   f,f2:Tfoo;
  29.   b:byte;
  30. begin
  31.   writeln(f.v);
  32.   f2 := 10;
  33.   writeln(f2.v);
  34.   b := f;
  35.   writeln(b);
  36. end.

This should get you on the right track.
« Last Edit: January 14, 2022, 08:01:33 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

speter

  • Sr. Member
  • ****
  • Posts: 349
Re: default method for objects
« Reply #4 on: January 14, 2022, 07:54:10 am »
Thanks very much for your help Thaddy (and jamie)!

My original question was: can I do something like:
Code: Pascal  [Select][+][-]
  1. var
  2.   zz : tfoo;
  3.   k : byte;
  4. begin
  5.   zz.init;
  6.   k := zz;        // i'd like to do this - have it return 'v' or call getit()
  7. end;

is this possible?

cheers
S. :o
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: default method for objects
« Reply #5 on: January 14, 2022, 08:02:55 am »
Well I just demo'd that, isn't it? You should look at how I implemented the class operator Initialize.
That is fully automatic. So yes, it is possible. Also look at the two class operators.
  b := f;
  writeln(b);

This is exactly what you asked. b = byte and f = Tfoo.

Try to understand my - complete - example. It works as intended and with the correct syntax.
« Last Edit: January 14, 2022, 08:08:37 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

speter

  • Sr. Member
  • ****
  • Posts: 349
Re: default method for objects
« Reply #6 on: January 14, 2022, 08:28:37 am »
Yes! Thanks Thaddy.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: default method for objects
« Reply #7 on: January 14, 2022, 09:12:35 am »
I would also add the two comparison operators =
I leave that as an exercise for you.  :D
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018