Recent

Author Topic: Multiple type helpers choice  (Read 3104 times)

Okoba

  • Full Member
  • ***
  • Posts: 247
Multiple type helpers choice
« on: July 05, 2021, 04:46:04 pm »
Hello,

I have two units, each declaring their helpers, one method has the same name and params, so the later unit is overriding the first one.
If I had these codes in a unit methods, there was no problem if I needed to choose one in a case. Choosing the unit name before method name solved it. But what about type helpers? Is there a way for me to choose the Unit1 helper while I have Unit2?

Please read the sample for more info.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode ObjFPC}{$H+}
  4. {$modeswitch typehelpers}
  5. {$modeswitch multihelpers}
  6.  
  7. interface
  8.  
  9. type
  10.   TTestHelper1 = type helper for String
  11.     procedure Test; overload;
  12.   end;
  13.  
  14. procedure Test; overload;
  15.  
  16. implementation
  17.  
  18. procedure Test;
  19. begin
  20.   WriteLn('Unit1');
  21. end;
  22.  
  23. procedure TTestHelper1.Test;
  24. begin
  25.   WriteLn('TTestHelper1');
  26. end;
  27.  
  28. end.

Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. {$mode ObjFPC}{$H+}
  4. {$modeswitch typehelpers}
  5. {$modeswitch multihelpers}
  6.  
  7. interface
  8.  
  9. type
  10.   TTestHelper2 = type helper for String
  11.     procedure Test; overload;
  12.   end;
  13.  
  14. procedure Test; overload;
  15.  
  16. implementation
  17.  
  18. procedure Test;
  19. begin
  20.   WriteLn('Unit2');
  21. end;
  22.  
  23. procedure TTestHelper2.Test;
  24. begin
  25.   WriteLn('TTestHelper2');
  26. end;
  27.  
  28. end.
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode ObjFPC}{$H+}
  4. {$modeswitch typehelpers}
  5. {$modeswitch multihelpers}
  6. uses
  7.   Unit1,
  8.   Unit2;
  9.  
  10. var
  11.   T: String;
  12. begin
  13.   Test; //WriteLn('Unit2');
  14.   Unit1.Test; //WriteLn('Unit1');
  15.   T.Test; //WriteLn('TTestHelper2');
  16.   //Is there a way to trigger Unit1.TTestHelper1.Test?
  17.   ReadLn;
  18. end.          

devEric69

  • Hero Member
  • *****
  • Posts: 572
Re: Multiple type helpers choice
« Reply #1 on: July 05, 2021, 05:17:46 pm »
Hello,

AFAIK, only one type helper per type (string, here) is possible: it's the one in the first uses will should be used.
Put all in the same type helper, or modify your design: for example, create a library of string oriented class methods, or a TClass with string oriented methods that you can decorate afterwards, ...

A "type helper for" is a sort of not very "clean" hack of the type, amho.
« Last Edit: July 05, 2021, 05:25:47 pm by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Okoba

  • Full Member
  • ***
  • Posts: 247
Re: Multiple type helpers choice
« Reply #2 on: July 05, 2021, 05:30:13 pm »
Multi helpers are allowed thanks to FPC team. Notice the mode switches.

devEric69

  • Hero Member
  • *****
  • Posts: 572
Re: Multiple type helpers choice
« Reply #3 on: July 05, 2021, 05:47:27 pm »
Notice the mode switches.

Ah! Ok, my knowledge may be outdated on this subject :-[: I didn't know that ( however, take a look at this thread: https://forum.lazarus.freepascal.org/index.php/topic,55150.msg409975.html#msg409975: according to PascalDragon "Only one helper can be active for a type at the same time." ).
« Last Edit: July 05, 2021, 05:49:50 pm by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Okoba

  • Full Member
  • ***
  • Posts: 247
Re: Multiple type helpers choice
« Reply #4 on: July 05, 2021, 06:06:35 pm »
And he said with the mode switch it can be achieved.
As probably PascalDragon will see this, if it is not possible to somehow choose the preferred method of preferred helper, can I ask if it can be possible in the future, what the syntax will be like?

jamie

  • Hero Member
  • *****
  • Posts: 4848
Re: Multiple type helpers choice
« Reply #5 on: July 05, 2021, 08:08:06 pm »
always put the most advanced helper at the end of the USES list, that will be seen first.

when extending a helper inherit from the original.. etc.

 you really shouldn't have two different helpers for the same type within reach..

The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 3338
  • Compiler Developer
Re: Multiple type helpers choice
« Reply #6 on: July 06, 2021, 03:35:49 pm »
I have two units, each declaring their helpers, one method has the same name and params, so the later unit is overriding the first one.
If I had these codes in a unit methods, there was no problem if I needed to choose one in a case. Choosing the unit name before method name solved it. But what about type helpers? Is there a way for me to choose the Unit1 helper while I have Unit2?

You need to order your units accordingly. As jamie implied the compiler looks from last used unit to first and that's the order in which the helpers will be used.

Notice the mode switches.

Ah! Ok, my knowledge may be outdated on this subject :-[: I didn't know that ( however, take a look at this thread: https://forum.lazarus.freepascal.org/index.php/topic,55150.msg409975.html#msg409975: according to PascalDragon "Only one helper can be active for a type at the same time." ).

How about you read my full post? Especially this part (emphasis mine):

(Note: In FPC trunk one can also use the modeswitch MultiHelpers to allow for multpile helpers of the same type without the need to use inheritance, though the released FPC versions do not support that)

A "type helper for" is a sort of not very "clean" hack of the type, amho.

Don't know what you mean with that. Type helpers are a fully supported, first class feature.

you really shouldn't have two different helpers for the same type within reach..

The MultiHelpers modeswitch is exactly to allow multiple helpers for the same type.

Okoba

  • Full Member
  • ***
  • Posts: 247
Re: Multiple type helpers choice
« Reply #7 on: July 06, 2021, 03:42:53 pm »
As probably PascalDragon will see this, if it is not possible to somehow choose the preferred method of preferred helper, can I ask if it can be possible in the future, what the syntax will be like?
PascalDragon, Can I have your comment on this question too?
I am mostly interested in what if you want to access both helpers from both units like when you choose the unit name before the function or class (example in the first post). IF it can be possible in the future, what the syntax will be like?

PascalDragon

  • Hero Member
  • *****
  • Posts: 3338
  • Compiler Developer
Re: Multiple type helpers choice
« Reply #8 on: July 06, 2021, 03:46:21 pm »
I did reply to it:

You need to order your units accordingly. As jamie implied the compiler looks from last used unit to first and that's the order in which the helpers will be used.

There will be no other way for this.

Okoba

  • Full Member
  • ***
  • Posts: 247
Re: Multiple type helpers choice
« Reply #9 on: July 06, 2021, 03:52:52 pm »
I was asking about a hypothetical syntax idea, but it seems you are saying that it will never happen.
Thank you.

 

TinyPortal © 2005-2018