[fpc-pascal] Overloading IN operator

Sven Barth pascaldragon at googlemail.com
Wed Dec 13 07:56:24 CET 2017


Am 13.12.2017 02:37 schrieb "Fabio Luis Girardi" <fluisgirardi at gmail.com>:

Hi all!

I want to extend some operators in Freepascal. I want to achieve something
like this:

if aStr in ['string item 1', 'string item 2', 'string item 3'] then
  ...
else
  ...;

Following the examples to overload operators, I wrote:

operator in (a:String; b:array of string):Boolean;
var
  c: Integer;
begin
  Result:=false;
  for c:=0 to High(b) do
    if b[c]=a then begin
      result:=true;
      exit;
    end;
end;

But when I try compile this piece of code:

if aStr in ['string item 1', 'string item 2', 'string item 3'] then
  ...
else
  ...;

I got:

Error: Ordinal expression expected


The problem is not the operator, but the array: before 3.1.1 the "[...]"
operator if used outside a parameter that is an open array *always*
generates a set. This only changed in trunk a few months ago so that the
operator can be used as an array constructor as well. So for now you'd need
to use an array variable on the right side.

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20171213/e27790d6/attachment.html>


More information about the fpc-pascal mailing list