Recent

Author Topic: How to Overload Method/Property Access Operator (Period Sign)  (Read 3775 times)

heejit

  • Full Member
  • ***
  • Posts: 245
How to Overload Method/Property Access Operator (Period Sign)
« on: November 13, 2018, 07:23:06 pm »
Code: Pascal  [Select][+][-]
  1. I have following class
  2.  
  3. _VDict       = specialize TFPGMap<string, variant>;
  4.  
  5. VDict = class(_VDict)
  6.   public
  7.     procedure print();
  8.     function copy(): VDict;
  9.   end;
  10.  
  11. Normal Way to Access Data of Key:
  12. VDict.KeyData['tran_date']
  13.  
  14. I want access data in following way
  15. VDict.tran_date
  16.  
  17.  
  18.  

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to Overload Method/Property Access Operator (Period Sign)
« Reply #1 on: November 13, 2018, 07:49:40 pm »
Add a property with that name? For example:

Code: Pascal  [Select][+][-]
  1. _VDict       = specialize TFPGMap<string, variant>;
  2.  
  3. VDict = class(_VDict)
  4.   private
  5.    function GetTran_date: {whatever_type};
  6.   public
  7.     procedure print();
  8.     function copy(): VDict;
  9.     property tran_date: {whatever_type} read GetTran_date;
  10.   end;
  11.  
  12. [. . .]
  13.  
  14. function VDict.GetTran_date: {whatever_type};
  15. begin
  16.   Result := VDict.KeyData['tran_date'] ;
  17. end;
  18.  

It isn't a working example--I don't know what type tran_data should be--but it should suffice you to get the drift..
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

heejit

  • Full Member
  • ***
  • Posts: 245
Re: How to Overload Method/Property Access Operator (Period Sign)
« Reply #2 on: November 14, 2018, 12:09:48 am »
Keys are not fixed VDict will be populated at runtime with different keys

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to Overload Method/Property Access Operator (Period Sign)
« Reply #3 on: November 14, 2018, 12:27:47 am »
Keys are not fixed VDict will be populated at runtime with different keys

Then AFAIK it can't be done. Remember that the compiler must know what each identifier refers to at compile-time.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

heejit

  • Full Member
  • ***
  • Posts: 245
Re: How to Overload Method/Property Access Operator (Period Sign)
« Reply #4 on: November 14, 2018, 09:42:07 am »
So there is no way to overload method access operator(Period Sign)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to Overload Method/Property Access Operator (Period Sign)
« Reply #5 on: November 14, 2018, 11:02:47 am »
So there is no way to overload method access operator(Period Sign)
look up custom variants..
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

heejit

  • Full Member
  • ***
  • Posts: 245
Re: How to Overload Method/Property Access Operator (Period Sign)
« Reply #6 on: November 14, 2018, 11:31:29 am »
@taazz

I search wiki : not found anything

Where to look for it?

If you can provide a link will be help full


Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: How to Overload Method/Property Access Operator (Period Sign)
« Reply #7 on: November 14, 2018, 12:11:44 pm »
Keys are not fixed VDict will be populated at runtime with different keys

Then AFAIK it can't be done. Remember that the compiler must know what each identifier refers to at compile-time.

As VDict is specialized with variant data type:
Code: Pascal  [Select][+][-]
  1. I have following class
  2.  
  3. _VDict       = specialize TFPGMap<string, variant>;
  4.  

Then the result of
Code: Pascal  [Select][+][-]
  1. VDict.KeyData['tran_date']
will be of type variant.

Therefore, the new property should be variant too:
Code: Pascal  [Select][+][-]
  1. property tran_date: Variant read GetTran_date;


taazz

  • Hero Member
  • *****
  • Posts: 5368
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

heejit

  • Full Member
  • ***
  • Posts: 245
Re: How to Overload Method/Property Access Operator (Period Sign)
« Reply #9 on: November 14, 2018, 01:38:07 pm »
I dont want it to complicate

If operator overloading is not possible
then better I stick to what is available.

Thanks

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: How to Overload Method/Property Access Operator (Period Sign)
« Reply #10 on: November 14, 2018, 04:22:55 pm »
I dont want it to complicate

If operator overloading is not possible
then better I stick to what is available.

Thanks
the period is not an operator if that is what you mean. It is just scoping: unit members, class members, record members, interface members, enums, etc.
Since scope is always compile time it can not be misused to turn it into a dynamic - scripting - language, which is what you are trying to do.
What you CAN do is use one of the many scripting languages that can be embedded in FreePascal. If these support classes (many do..), you can generate a scripted class on the fly and call with dot syntax....the scripting engine of choice...
- Pascal script
- JavaScript
- PHP
etc, etc..

I am now too busy with finishing the wiki entry for conditional compilation but I am sure I can come up with a PascalScript example later this week if you can't figure it out. (It is medium to complex code, but probably reusable)
« Last Edit: November 14, 2018, 04:39:17 pm by Thaddy »
Specialize a type, not a var.

heejit

  • Full Member
  • ***
  • Posts: 245
Re: How to Overload Method/Property Access Operator (Period Sign)
« Reply #11 on: November 14, 2018, 06:35:15 pm »
@Thaddy

Now it is clear that period is not operator thanks.

Your are correct I am trying to bring scripting language logic into this
but it is Use full in some situation.

One of the compiled language Xojo programming have operator_lookup function
you can override in a class if any member you are trying to access not found then this function
will called.

Script Example will be help full.

 

TinyPortal © 2005-2018