Recent

Author Topic: Basic quistion about FreePascal  (Read 2768 times)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Basic quistion about FreePascal
« Reply #15 on: May 26, 2020, 06:09:07 pm »
(but it will only work if all others are also marked overload;)
If only one unit has the "overload" directive, sequence in "uses" section is important. Compiler starts search from units which were mentioned last and if it finds a header with mismatching parameters and "overload", it continues a search. Otherwise not.

Indeed, as soon as it finds an overloaded version without the "overload" modifier it stops searching for more and considers that the "last" version, which can be easily ascertained with a simple test with at least three units, an overload per unit and only one of them having the overload modifier:

Code: Pascal  [Select][+][-]
  1. { In the first unit ...}
  2. function DoSomething(Value: Double): String;
  3. begin
  4.   Result := 'As Double: ' + FloatToStr(Value);
  5. end;
  6.  
  7. { In the second unit ...}
  8. function DoSomething(Value: Integer): String;
  9. begin
  10.   Result := 'As Integer: ' + IntToStr(Value);
  11. end;
  12.  
  13. { In the third one ...}
  14. function DoSomething(Value: String): String; overload;
  15. begin
  16.   Result := 'As String: ' + AnsiProperCase(Value, StdWordDelims);
  17. end;

For example, writing:
Code: [Select]
uses first, second, third; you can't call DoSomething(367.25); writing
Code: [Select]
uses second, first, third; and calling DoSomething(321) will convert the integer and use the "Double" overload.

So Marco was quite right and all the overloads (except the very first) should be declared with the overload modifier, as the docs (if somewhat cryptically) say. ;)
« Last Edit: May 26, 2020, 06:11:37 pm by lucamar »
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.

 

TinyPortal © 2005-2018