[fpc-devel] Overloading standard functions

Виктор Матузенко vitek03 at gmail.com
Wed Jun 19 13:46:10 CEST 2019


Hi!

I am writing my own string type and I want to overload standard string
functions.
I don't want to hide them, just overload.

Consider the following example (I use 3.0.4 [2017/10/06] for i386):

=======================================
{$MODE FPC}

type
TMyString = record
end;

function Pos(const Sub, S: TMyString): LongInt;
begin
  Pos := 0;
end;

function Length(const MyString: TMyString): SizeUInt;
begin
  Length := 0;
end;

function Copy(const MyString: TMyString; A, B: LongInt): TMyString;
begin
  Copy := MyString;
end;

begin
  // Works just fine
  Writeln(Pos('a', 'abcdef'));
  // Error: Incompatible type for arg no. 1: Got "Constant String",
expected "TMyString"
  Writeln(Copy('abcdef', 1, 4));
  // Error: Incompatible type for arg no. 1: Got "Constant String",
expected "TMyString
  Writeln(Length('abcdef'));
end.
=======================================

Pos behaves differently than Length and Copy.
Seems inconsistent -- should it be considered as a bug?

-- 
Victor Matuzenko (Виктор Матузенко)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190619/c5635e99/attachment.html>


More information about the fpc-devel mailing list