[fpc-pascal] modeswitch multihelpers precedence problem

Anthony Walter sysrpl at gmail.com
Tue Mar 10 05:57:12 CET 2020

I started adding {$modeswitch multihelpers} to some of my code and noticed
a problem. It would seem that the SysUtils unit is defining type helpers
for the string type and this led me to discover that multiple type helpers
takes precedence over other helpers in a way that is conflicts with other
pascal rules.

To demonstrate, if I have MyUnit.pas that defines a string type helper
with EndsWith() and an IntToStr() function then consider the following code:

program Test;

  MyUnit, SysUtils;

  'hello'.EndsWith('lo'); // invokes MyUnit.TStringHelper.EndsWith()
  IntToStr(12); // invokes SysUtils.IntToStr()

But if I reverse the uses order:

program Test;

  SysUtils, MyUnit;

  'hello'.EndsWith('lo'); // invokes SysUtils.TStringHelper.EndsWith()
  IntToStr(12); // invokes MyUnit.IntToStr()

What should happen is that both examples use the function from the last
unit in the uses clause. Using the example above with "uses SysUtils,
MyUnit;" the following should happen:

  'hello'.EndsWith('lo'); // invokes MyUnit.TStringHelper.EndsWith()
  IntToStr(12); // invokes MyUnit.IntToStr()

I know some of you might see this as a small problem, but it's an
inconstancy that can lead to a situation where the wrong methods will be
called without being obvious. The standard has long been that when an
identifier of the same name is is found, pascal will choose to identify the
name as the one coming from the last matching unit in the uses clause.
Multiple type helpers should follow this same rule.

Please discuss and if we all agree I will file this as a bug in mantis.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20200310/3bb9752a/attachment.html>

More information about the fpc-pascal mailing list